Dareros Posted September 17, 2014 Share Posted September 17, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/291114-equivalent-to-this-command-with-curl/ Share on other sites More sharing options...
requinix Posted September 17, 2014 Share Posted September 17, 2014 (edited) $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 September 17, 2014 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/291114-equivalent-to-this-command-with-curl/#findComment-1491358 Share on other sites More sharing options...
Dareros Posted September 17, 2014 Author Share Posted September 17, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/291114-equivalent-to-this-command-with-curl/#findComment-1491417 Share on other sites More sharing options...
Jacques1 Posted September 17, 2014 Share Posted September 17, 2014 Read requinix' reply. There is no such thing as $http_response_header for cURL. You need to manually extract the headers from the response. Quote Link to comment https://forums.phpfreaks.com/topic/291114-equivalent-to-this-command-with-curl/#findComment-1491422 Share on other sites More sharing options...
Dareros Posted September 17, 2014 Author Share Posted September 17, 2014 (edited) 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 September 17, 2014 by Dareros Quote Link to comment https://forums.phpfreaks.com/topic/291114-equivalent-to-this-command-with-curl/#findComment-1491425 Share on other sites More sharing options...
Jacques1 Posted September 17, 2014 Share Posted September 17, 2014 google.com is a bad example, because they're doing all kinds of magic. The question is: Do you have any cURL problems with the real target page? Quote Link to comment https://forums.phpfreaks.com/topic/291114-equivalent-to-this-command-with-curl/#findComment-1491430 Share on other sites More sharing options...
Dareros Posted September 17, 2014 Author Share Posted September 17, 2014 Okay, i haven't tested it locally yet. So if both are identical and it is only google.com magic than no problem. But i haven't tried it yet to answer you. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/291114-equivalent-to-this-command-with-curl/#findComment-1491458 Share on other sites More sharing options...
abdul202 Posted September 21, 2014 Share Posted September 21, 2014 (edited) 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 September 21, 2014 by abdul202 Quote Link to comment https://forums.phpfreaks.com/topic/291114-equivalent-to-this-command-with-curl/#findComment-1491742 Share on other sites More sharing options...
Jacques1 Posted September 21, 2014 Share Posted September 21, 2014 We already knew that, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/291114-equivalent-to-this-command-with-curl/#findComment-1491745 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.