Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get Body content of HTTPS using CURL

Tags:

php

curl

https

The following code will retrieve the body content of a url retrieved using CURL in php but not https. Can anyone tell me how I edit the code as I need to get the data returned not just the header.

From the test I did here is the result. You can see it has a content-length, I just don't know how to access it.

Thanks Stephen

Errors: 0

string(1457) "HTTP/1.1 200 OK Date: Sat, 01 Aug 2009 06:32:11 GMT Server: Apache/1.3.41 (Darwin) PHP/5.2.4 mod_ssl/2.8.31 OpenSSL/0.9.7l Cache-Control: max-age=60 Expires: Sat, 01 Aug 2009 06:33:11 GMT Last-Modified: Thu, 23 Nov 2006 17:44:53 GMT ETag: "97d620-44b-4565de15" Accept-Ranges: bytes Content-Length: 1099 Connection: close Content-Type: text/html "

<?php

$curl_handle=curl_init();

$username = "";
$password = "";

$fullurl = "http://www.queensberry.com";
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_HEADER, 1);
   curl_setopt($ch, CURLOPT_VERBOSE, 1);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
   curl_setopt($ch, CURLOPT_FAILONERROR, 0);
   curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
   curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
   curl_setopt($ch, CURLOPT_URL, $fullurl);

   $returned = curl_exec($ch);

   curl_close ($ch);
   var_dump($returned);


?>
like image 870
Stephen Baugh Avatar asked Aug 01 '09 06:08

Stephen Baugh


People also ask

How to send HTTP get request using cURL?

To make a GET request using Curl, run the curl command followed by the target URL. Curl automatically selects the HTTP GET request method unless you use the -X, --request, or -d command-line option. In this Curl GET example, we send Curl requests to the ReqBin echo URL.

How to cURL HTTP?

cURL makes HTTP requests just like a web browser. To request a web page from the command line, type curl followed by the site's URL: The web server's response is displayed directly in your command-line interface. If you requested an HTML page, you get the page source -- which is what a browser normally sees.

How to specify get method in cURL?

The GET method is used to retrieve resources from a particular URL. The simple curl https://www.keycdn.com command will use GET as the default HTTP method, however it can also be specified using --request GET or -X GET .

What is cURL F option?

-F tells curl to emulate a filled in HTML Form that has just had it's submit button clicked.


2 Answers

Here is the solution: Try this, just keep rest of the coding same as above...

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
// curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_URL, $fullurl);

$returned = curl_exec($ch);

curl_close ($ch);
var_dump($returned);

Changing CURLOPT_HEADER to 0 makes it so that only the page content is returned.

like image 176
Dinesh Avatar answered Sep 28 '22 18:09

Dinesh


I look for the length of the header using the curl's getinfo. Then substring the response:

$info = curl_getinfo($ch);
$start = $info['header_size'];
$body = substr($result, $start, strlen($result) - $start);
like image 35
fil Avatar answered Sep 28 '22 18:09

fil