Skip to content

Instantly share code, notes, and snippets.

@alash3al
Last active February 23, 2023 18:13
Show Gist options
  • Save alash3al/aafe8ffa0a6ca12eb35f to your computer and use it in GitHub Desktop.
Save alash3al/aafe8ffa0a6ca12eb35f to your computer and use it in GitHub Desktop.

Revisions

  1. alash3al revised this gist Feb 13, 2015. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions wget.php
    Original 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 "onSuccess" | string "onFailur"
    * @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))) !== FALSE ) {
    $headers = substr($response, 0, $post);
    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' => $headers, 'body' => $body );
    return array( 'headers' => trim($headers), 'body' => trim($body) );
    }

    // -------------------

    // example:
    var_dump(wget('google.com', 'GET', array('Cookie: key1=value1; key2=value2')));
    var_dump(wget('google.com', 'POST', array(), 'xx,cc.vv'));
  2. alash3al revised this gist Feb 13, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion wget.php
    Original 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|string
    *
    * @return array "onSuccess" | string "onFailur"
    */
    function wget($url, $method = 'GET', array $headers = array(), $body = '')
    {
  3. alash3al revised this gist Feb 13, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions wget.php
    Original 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')));
  4. alash3al renamed this gist Feb 13, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. alash3al created this gist Feb 13, 2015.
    83 changes: 83 additions & 0 deletions wget
    Original 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')));