Skip to content

Instantly share code, notes, and snippets.

@alash3al
Last active August 29, 2015 14:15
Show Gist options
  • Save alash3al/fbe96ad6c061fed1f198 to your computer and use it in GitHub Desktop.
Save alash3al/fbe96ad6c061fed1f198 to your computer and use it in GitHub Desktop.
wget(), version 2, a PHP curl alternative
<?php
/**
* wget, the curl alternative
*
* @author Mohammed Al Ashaal <is.gd/alash3al>, <fb.com/alash3al>
* @version 2.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' => 'k=v; x=y' )
* @param string $body only if the $methd is not GET
*
* @return object "success" | string "failure"
*/
function wget($url, $method = 'GET', array $headers = array(), $body = '')
{
// get the url components
$url = (object) array_merge(array(
'scheme' => 'http',
'host' => '',
'port' => 80,
'path' => '/',
'query' => ''
), parse_url($url));
// PHP sets the host empty and the path to host
// only if the given url is [just.host]
// so we must fix it .
if ( empty($url->host) ) {
$url->host = $url->path;
$url->path = '/';
}
// the scheme
if ( $url->scheme )
{
if ( strtolower($url->scheme) == 'http' ) {
$url->scheme = 'tcp';
$url->port = 80;
}
elseif ( strtolower($url->scheme) == 'https' ) {
$url->scheme = 'ssl';
$url->port = 443;
}
}
// open socket connection
$fp = $socket = fsockopen(($url->scheme ? $url->scheme . '://' : '') . $url->host, $url->port, $errno, $errstr, 10);
// 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 ? ('?' . $url->query) : null),
'host' => $url->host . ':' . $url->port,
'user-agent' => 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Wget/2.0 Safari/537.36 (Mohammed Al Ashaal <fb.com/alash3al>)',
'accept' => '*/*',
'accept-language' => 'en-US,en;q=0.8',
'connection' => 'Close',
'x-real-client' => 'MohammedAlashaal/Wget'
), array_change_key_case($headers, CASE_LOWER));
// coninue for non-get methods
if ( strtolower($method) !== 'get' )
{
$headers['content-length'] = strlen($body);
$headers[] = '';
$headers[] = '';
$headers[] = $body;
}
else $headers[] = '';
// the headers string
$h = '';
// generate the headers string
foreach ( $headers as $k => $v )
{
if ( is_int($k) )
$h .= $v . PHP_EOL;
else
$h .= sprintf('%s: %s', str_replace(' ', '-', ucwords(strtolower(str_replace('-', ' ', $k)))), $v) . PHP_EOL;
unset($headers[$k]);
}
// write the headers to the target
fwrite($socket, $h);
// headers and body from the server
$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);
// tokenize
$k = strtok($headers, PHP_EOL);
// headers array
$headers = array();
// the status line
@list(, $headers['status_code'], ) = explode(' ', $k, 3);
$k = strtok(PHP_EOL);
// decode-chunked
if ( !empty($headers['transfer_encoding']) && $headers['transfer_encoding'] == 'chunked' )
{
$pos = 0;
$len = strlen($body);
while ( $pos < $len )
{
$chuncked = substr($body, $pos, strpos($body, PHP_EOL));
if ( ctype_xdigit($chuncked) )
die("chuncked");
var_dump($chuncked, $body);exit;
}
}
// some default header fields
$headers['status_code'] = (int) $headers['status_code'];
$headers['url'] = $url;
// return the result
return (object) array( 'headers' => (object) $headers, 'body' => trim($body) );
}
@alash3al
Copy link
Author

Example #1

<?php

    // reading news about cyber security from google news
    $google = wget('https://ajax.googleapis.com/ajax/services/search/news?v=1.0&q=cybersecurity');

    /*
        returns an object :
        headers ->  the response headers as key value pairs object
                    the headers fields are lowercased .
                    ----------------------------------------------
        body    ->  the response body string [the web page].
    */

    // the headers has a location key then
    // we must go to it . [redirect]
    if ( isset($google->headers->location) ) {
        $google = wget($google->headers->location);
    }

    // if the url is json page
    if ( preg_match('/(json|javascript)/', $google->headers->content_type) ) {
        $google->body = json_decode($google->body);
    }

    // dump body
    var_dump($google->body);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment