How to Get Body content of HTTPS using CURL

23,808

Solution 1

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.

Solution 2

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);

Solution 3

Shouldn't $fullurl be "https://www.queensberry.com" ?

When I changed $fullurl as stated and ran the code, var_dump displayed the "under construction" page.

Solution 4

if you still need the header, which means setting CURLOPT_HEADER to 0 is not an option, you can find the start of the body by looking for an empty line (two CRLF). See the spec: http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html

so this should do the job:

    $data = curl_exec($ch);
    $start = strpos($data, "\r\n\r\n") + 4;
    $body = substr($data, $start, strlen($data) - $start);
Share:
23,808
Stephen Baugh
Author by

Stephen Baugh

Click here to see the about page on my blog

Updated on July 09, 2022

Comments

  • Stephen Baugh
    Stephen Baugh almost 2 years

    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);
    
    
    ?>