Last active
February 23, 2023 18:13
-
-
Save alash3al/aafe8ffa0a6ca12eb35f to your computer and use it in GitHub Desktop.
Revisions
-
alash3al revised this gist
Feb 13, 2015 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,7 @@ * @param array $headers example array( 'Cookie: k1=v1; k2=v2' ) * @param string $body only if the $methd is not GET * * @return array "success" | string "failur" */ function wget($url, $method = 'GET', array $headers = array(), $body = '') { @@ -67,19 +67,19 @@ function wget($url, $method = 'GET', array $headers = array(), $body = '') $headers = ''; $body = ''; // generating the headers and the body if ( ($pos = strpos($response = stream_get_contents($socket), PHP_EOL . PHP_EOL)) !== FALSE ) { $headers = substr($response, 0, $pos); $body = substr($response, $pos + 1); } else $headers = $response; // close the socket connection fclose($socket); // return the result return array( 'headers' => trim($headers), 'body' => trim($body) ); } // ------------------- // example: var_dump(wget('google.com', 'POST', array(), 'xx,cc.vv')); -
alash3al revised this gist
Feb 13, 2015 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,8 @@ * @param string $method example 'GET' * @param array $headers example array( 'Cookie: k1=v1; k2=v2' ) * @param string $body only if the $methd is not GET * * @return array "onSuccess" | string "onFailur" */ function wget($url, $method = 'GET', array $headers = array(), $body = '') { -
alash3al revised this gist
Feb 13, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -80,4 +80,5 @@ function wget($url, $method = 'GET', array $headers = array(), $body = '') // ------------------- // example: var_dump(wget('google.com', 'GET', array('Cookie: key1=value1; key2=value2'))); -
alash3al renamed this gist
Feb 13, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
alash3al created this gist
Feb 13, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,83 @@ <?php /** * wget just like linux wget command * * @author Mohammed Al Ashaal <is.gd/alash3al> * @version 1.0.0 * @license MIT License * * @param string $url example 'http://site.com/xxx?k=v' * @param string $method example 'GET' * @param array $headers example array( 'Cookie: k1=v1; k2=v2' ) * @param string $body only if the $methd is not GET * @return array|string */ function wget($url, $method = 'GET', array $headers = array(), $body = '') { // prevent timeout set_time_limit(0); // get the url components $url = (object) array_merge(array( 'scheme' => '', 'host' => '', 'port' => 80, 'path' => '/', 'query' => '' ), parse_url($url)); if ( empty($url->host) ) { $url->host = $url->path; $url->path = '/'; } // open socket connection $socket = fsockopen($url->host, $url->port, $errno, $errstr, 30); // if there is any error // exit and print its string if ( $errno ) return $errstr; // generate the headers $headers = array_merge(array ( sprintf('%s %s?%s HTTP/1.1', strtoupper($method), $url->path, $url->query), sprintf('Host: %s', $url->host), 'Connection: Close' ), $headers); // coninue for non-get methods if ( strtolower($method) !== 'get' ) { $headers[] = sprintf('Content-Length: %s', strlen($body)); $headers[] = ''; $headers[] = ''; $headers[] = $body; } else $headers[] = ''; $headers = join(PHP_EOL, $headers) . PHP_EOL; // write the headers to the target fwrite($socket, $headers); // headers and body from the server $headers = ''; $body = ''; // generating the headers and the body if ( ($pos = strpos($response = stream_get_contents($socket))) !== FALSE ) { $headers = substr($response, 0, $post); $body = substr($response, $pos + 1); } else $headers = $response; // close the socket connection fclose($socket); // return the result return array( 'headers' => $headers, 'body' => $body ); } // ------------------- var_dump(wget('google.com', 'GET', array('Cookie: key1=value1; key2=value2')));