php curl example

rgb192
rgb192 used Ask the Experts™
on
Want to enter page URL as input and return view source html of page.  

tried attached example

is this the proper output

CURL FAIL: TIMEOUT=3, CURL_ERRNO=3array(21) { ["url"]=>  string(0) "" ["content_type"]=>  NULL ["http_code"]=>  int(0) ["header_size"]=>  int(0) ["request_size"]=>  int(0) ["filetime"]=>  int(-1) ["ssl_verify_result"]=>  int(0) ["redirect_count"]=>  int(0) ["total_time"]=>  float(0) ["namelookup_time"]=>  float(0) ["connect_time"]=>  float(0) ["pretransfer_time"]=>  float(0) ["size_upload"]=>  float(0) ["size_download"]=>  float(0) ["speed_download"]=>  float(0) ["speed_upload"]=>  float(0) ["download_content_length"]=>  float(-1) ["upload_content_length"]=>  float(-1) ["starttransfer_time"]=>  float(0) ["redirect_time"]=>  float(0) ["certinfo"]=>  array(0) { } }

or what lines should i change
<?php // RAY_curl_get_example.php
error_reporting(E_ALL);


// DEMONSTRATE THE BASICS OF CURL
// SOMETHING LIKE RAY_curl_get_example.php?url=http://twitter.com


$htm = my_curl($_GET["url"]);

echo "<pre>";
echo PHP_EOL . $_GET["url"];
echo PHP_EOL . htmlentities($htm);
echo PHP_EOL;


// A FUNCTION TO RUN A CURL-GET CLIENT CALL TO A FOREIGN SERVER
function my_curl
( $url
, $timeout=3
, $error_report=TRUE
)
{
    $curl = curl_init();

    // HEADERS AND OPTIONS APPEAR TO BE A FIREFOX BROWSER REFERRED BY GOOGLE
    $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = "Pragma: "; // BROWSERS USUALLY LEAVE BLANK

    // SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
    curl_setopt( $curl, CURLOPT_URL,            $url  );
    curl_setopt( $curl, CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'  );
    curl_setopt( $curl, CURLOPT_HTTPHEADER,     $header  );
    curl_setopt( $curl, CURLOPT_REFERER,        'http://www.google.com'  );
    curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate'  );
    curl_setopt( $curl, CURLOPT_AUTOREFERER,    TRUE  );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE  );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE  );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout  );

    // RUN THE CURL REQUEST AND GET THE RESULTS
    $htm = curl_exec($curl);

    // ON FAILURE HANDLE ERROR MESSAGE
    if ($htm === FALSE)
    {
        if ($error_report)
        {
            $err = curl_errno($curl);
            $inf = curl_getinfo($curl);
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            var_dump($inf);
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS RETURN XML / HTML STRING
    curl_close($curl);
    return $htm;
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Erdinç Güngör ÇorbacıPHP Development Team Leader

Commented:
You may use get_file_contents() instead if you don't need to do special things with headers.

http://php.net/manual/en/function.file-get-contents.php

PHP Development Team Leader
Commented:
and also your curl code works i think

Just save what you've sent us as -for instance- mycurl.php

and for http://www.facebook.com/login.php call it like

http://localhost/mycurl.php?url=http://www.facebook.com/login.php

but facebook understands you are not the one he expects from headers (i guess :) )
Most Valuable Expert 2011
Top Expert 2016
Commented:
Line 20 provides the default timeout value in seconds:

, $timeout=3

The message you got said:
CURL FAIL: TIMEOUT=3, CURL_ERRNO=3

This message was created on line 56:

echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";

CURL ERRNO 3 is "malformed URL" and since there is NO URL in the message, it appears that the guidance on line 6 was overlooked:
// SOMETHING LIKE RAY_curl_get_example.php?url=http://twitter.com

Try the script again with a valid URL in the GET argument as shown in the example.  I am fairly sure the script works correctly if you give it the URL it needs.  My server is down at the moment, but when it comes back up you can see the script demonstrated here:
http://www.laprbass.com/RAY_curl_get_example.php?url=http://twitter.com

Author

Commented:
thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial