• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

php curl

Want to enter page URL as input and return view source html of page.  
0
rgb192
Asked:
rgb192
2 Solutions
 
Greg AlexanderLead DeveloperCommented:
Here is a simple way

<?
echo file_get_contents("http://google.com");
?>

Open in new window

0
 
Loganathan NatarajanLAMP DeveloperCommented:
You can try with cURL.
ref. Simple Example
0
 
Ray PaseurCommented:
I answered something like this recently...
<?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

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
kumaranmcaCommented:
There are two ways url based details get. Refer the below code...

1. CURL
2. get_file_content() function

http://davidwalsh.name/download-urls-content-php-curl

$returned_content = get_data('http://davidwalsh.name');


/* gets the data from a URL */
function get_data($url)
{
      $ch = curl_init();
      $timeout = 5;
      curl_setopt($ch,CURLOPT_URL,$url);
      curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
      curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
      $data = curl_exec($ch);
      curl_close($ch);
      return $data;
}
0
 
rgb192Author Commented:
tried Ray's 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
0
 
rgb192Author Commented:
two best answers thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now