Getting cURL to work in PHP with HTTPS

I have the following code that works in test regions that do not use SSL, but not on the production system, which does. The call itself works in the browser, but when I start via cURL, I get an error of 500.

$region = "https://api.mysite.com/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . "/cacert.pem");
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_URL, $region . $api);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$resp = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch); 

return $resp; 

If I do not put CURL_OPT_FAILONERROR, then I get no error, just an empty answer. I am sure that this is due to the fact that this happens via https (since this is the only difference between my test region and my current region), but I cannot figure out how to make this work.

+3
source share
4 answers

To confirm that the first problem is ssl related or not, check with

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Does it work with this?

+1
source

, , , . :

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

, "" , .

!

0

-

<?php

error_reporting(E_ALL);

ini_set("display_errors", 1);

$data = array(
    CURLOPT_CAINFO => "/Users/davidmann/Documents/facebook.pem", 
    CURLOPT_SSL_VERIFYHOST => 1,
    CURLOPT_SSL_VERIFYPEER => true
);

echo var_dump(curl_get('https://www.facebook.com/', array('Refferer' => 'https://www.facebook.com/'), $data));

/** 
 * Send a GET requst using cURL 
 * @param string $url to request 
 * @param array $get values to send 
 * @param array $options for cURL 
 * @return string 
 * @author David from Code2Design.com
 * @link http://au.php.net/manual/en/function.curl-exec.php#98628
 */ 
function curl_get($url, array $get = NULL, array $options = array()) 
{    
    $defaults = array( 
        CURLOPT_URL => $url. (strpos($url, '?') === FALSE ? '?' : ''). http_build_query($get), 
        CURLOPT_HEADER => 1, 
        CURLOPT_RETURNTRANSFER => TRUE, 
        CURLOPT_TIMEOUT => 4 
    ); 

    $ch = curl_init(); 
    curl_setopt_array($ch, ($options + $defaults)); 
    if( ! $result = curl_exec($ch)) 
    { 
        trigger_error(curl_error($ch)); 
    } 
    curl_close($ch); 
    return $result; 
} 

And then download the certificate from the website you are accessing (open firefox, go to https://site.com , right-click, view page information, security tab, certificate, select the topmost certificate, export as x .509 and then use the updateCURLOPT_CAINFO

Hope this helps, Dave

0
source

Try setting CURLOPT_REFERER and CURLOPT_USERAGENT. I had the same problem and that solved it.

0
source

All Articles