Jump to content

Recommended Posts

Hi, i would like to translate this code to request the url http://www.google.com and get header response with file_get_contents into cUrl command. This is the file_get_contents version :

<?php
$options = array( 'http' => array(
    'user_agent'    => $_SERVER['HTTP_USER_AGENT'],
  'max_redirects' => 7,
  'timeout'       => 120,
  'follow_location' => false
) );
$context = stream_context_create( $options );
$page    = @file_get_contents( 'http://www.google.com', false, $context );
print_r($http_response_header);

echo $page;
?>

Now, i have tried to translate it using cUrl with the code below but i don't get the same result, for example $http_response_header is void and you can test it yourself to see the difference :

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);

$page = curl_exec($ch);
curl_close($ch);
print_r($http_response_header);
echo $page;
?>

Thank you.

Link to comment
https://forums.phpfreaks.com/topic/291114-equivalent-to-this-command-with-curl/
Share on other sites

$http_response_header is only set when using PHP's built-in functions, like file_get_contents() or fopen().

 

If you want to get the headers with cURL then you'll need other options. I don't remember a nicer way of getting it than CURLOPT_NOBODY, CURLOPT_GET, and CURLOPT_HEADER, and parsing the headers returned...

Edited by requinix

Okay, i have resolved the problem of headers so now i can receive headers information but why the two functions below doesn't return the same result, just copy past it and test it yourself. I want to make the function test2 identical to test1 so i test one function and command out the other to see the result.

<?php

function test1() {
$options = array( 'http' => array(
	'user_agent'    => $_SERVER['HTTP_USER_AGENT'],    // who am i
  'max_redirects' => 7,          // stop after 10 redirects
  'timeout'       => 120,         // timeout on response
  'follow_location' => true
) );
$context = stream_context_create( $options );


$page    = @file_get_contents( 'http://www.google.com', false, $context );
print_r($http_response_header);
echo $page;
}

function test2() {
$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://www.google.com');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_NOBODY, false);
		curl_setopt($ch, CURLOPT_GET, true);
		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLOPT_MAXREDIRS, 7);
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt ($ch, CURLOPT_COOKIEFILE, "./cookie.txt");
    curl_setopt ($ch, CURLOPT_COOKIEJAR, "./cookie.txt");

    $output = curl_exec($ch);
    curl_close($ch);
		print_r($http_response_header);
    echo $output;
}

//test1();
test2();

?>

Thank you

Read requinix' reply. There is no such thing as $http_response_header for cURL. You need to manually extract the headers from the response.

Hi, the issue now is not the response header. It is the page content itself which render differently using both functions. I have modified the code so you can test it on google and see that the resulting page for the fule_get_contents version with test1 have the google black toolbar on the top while the curl version with test2 doesn't have it why ?

<?php

function test1() {
$options = array( 'http' => array(
	'user_agent'    => $_SERVER['HTTP_USER_AGENT'],    // who am i
  'max_redirects' => 7,          // stop after 10 redirects
  'timeout'       => 120,         // timeout on response
  'follow_location' => true
) );
$context = stream_context_create( $options );


$page    = @file_get_contents( 'http://www.google.com', false, $context );
echo $page;
}

function test2() {
$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://www.google.com');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		//curl_setopt($ch, CURLOPT_NOBODY, 0);
		//curl_setopt($ch, CURLOPT_GET, 0);
		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLOPT_MAXREDIRS, 7);
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt ($ch, CURLOPT_COOKIEFILE, "./cookie.txt");
    curl_setopt ($ch, CURLOPT_COOKIEJAR, "./cookie.txt");
		curl_setopt($ch, CURLOPT_AUTOREFERER, true);

    $response = curl_exec($ch);

		$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
		$header = substr($response, 0, $header_size); // Extract header info
		$body = substr($response, $header_size); // extract html body of the page

    curl_close($ch);
    echo $body; // print the html response
}

test1();
//test2();

?>

Thanks

Edited by Dareros

if you want to get the header and igonre the page body you must do this

curl_setopt($ch, CURLOPT_NOBODY,  1); // to not to get the body content
curl_scurl_setopt($ch, CURLOPT_NOBODY,  1); // to get the header response content

the full code which work with google as the follwing

$url = "https://www.google.com.eg/"; // From URL to get webpage
$ch = curl_init(); // Initialize a CURL session.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Return Page contents.
curl_setopt($ch, CURLOPT_URL, $url); // Pass URL as parameter.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
curl_setopt($ch, CURLOPT_NOBODY,  1);
curl_setopt($ch, CURLOPT_HEADER,  1);
$result = curl_exec($ch); // grab URL and pass it to the variable.
curl_close($ch); // close curl resource, and free up system resources.
var_dump($result); // Print page contents.

 

the response is

string 'HTTP/1.1 200 OK
Date: Sun, 21 Sep 2014 21:09:40 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=windows-1256
Set-Cookie: PREF=ID=6cd5c46f6c60ff0b:FF=0:TM=1411333780:LM=1411333780:S=col9O7TITLf9wxEY; expires=Tue, 20-Sep-2016 21:09:40 GMT; path=/; domain=.google.com.eg
Set-Cookie: NID=67=PYZc7YhsYGIak_wYUXdGB1krIqrqWimkef02XnRC8ulvcTbAeoZYdn06B-BtgQsCfXfNFPX_EgXkd5ZPGW4klvXJiueR4cOn6shMJltfTLJy6FfR6oVqT30fzp5dJCH6; expires=Mon, 23-Mar-2015 21:09:40 GMT; path=/; domain='... (length=810)
Edited by abdul202
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.