卷曲功能不起作用?

时间:2014-11-21 06:32:17

标签: php curl

我正在尝试使用curl方法从网站获取数据。在我能够通过使用curl获取数据之前。从那时起,客户端为该站点获取了ssl证书,我无法使用curl获取数据。但是当我在浏览器中尝试URL时,我能够看到数据。任何人都可以告诉我为什么它不工作。我需要以另一种方式尝试。 以下是我的代码

// create curl resource
    $ch = curl_init();

    // set url
    curl_setopt($ch, CURLOPT_URL, "https://qa.myhealth.today/myhealth-portal/nirvahak/public/validateSessionttt?username=pavithra@gmail.com");

    //return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // $output contains the output string
    $output = curl_exec($ch);

    // close curl resource to free up system resources
    curl_close($ch);  
  $output=json_decode($output);
     print_r($output);

2 个答案:

答案 0 :(得分:0)

有两个修复: 1)SSL认证的网站不信任任何非SSL请求。因此,我们需要赢得SSL客户端的信任。

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

2)向他提供安全证书。

进入相应的网站。保存SSL证书并将证书路径传递给CURL请求。

所以,最终的代码应该是:

// create curl resource
    $ch = curl_init();
$url = "https://qa.myhealth.today/myhealth-portal/nirvahak/public/validateSessionttt?username=pavithra@gmail.com";

    // set url
    curl_setopt($ch, CURLOPT_URL, $url);

    //return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/CAcerts/BuiltinObjectToken-EquifaxSecureCA.crt");
    // $output contains the output string
    $output = curl_exec($ch);

    // close curl resource to free up system resources
    curl_close($ch);  
  $output=json_decode($output);
     print_r($output);

有关如何操作的详细说明,请点here

答案 1 :(得分:0)

CURLOPT_SSL_VERIFYHOSTCURLOPT_SSL_VERIFYPEER设为false。

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

<强> CURLOPT_SSL_VERIFYHOST

1 to check the existence of a common name in the SSL peer certificate. 2 to check the existence of a common name and also verify that it matches the hostname provided. In production environments the value of this option should be kept at 2 (default value). 

<强> CURLOPT_SSL_VERIFYPEER

FALSE to stop cURL from verifying the peer's certificate. Alternate certificates to verify against can be specified with the CURLOPT_CAINFO option or a certificate directory can be specified with the CURLOPT_CAPATH option. 

来源:http://php.net/manual/en/function.curl-setopt.php