【问题标题】:Unable to set certificate and keystore details in php curl无法在 php curl 中设置证书和密钥库详细信息
【发布时间】:2013-12-20 08:32:19
【问题描述】:

我在 curl 中有以下代码来验证用户身份。

<?php
    $url='https://mysite';
       echo "url -->$url" . "\n";
    $credentials=array('uid'=>'111','password'=>'222');
       print_r($credentials); echo "\n";
    $headers=array('contentType:application/json','MY-API-Key:#$@SDsfsdfsdfsdfsfsf334234');
       print_r($headers); echo "\n";
    $keyFile=file_get_contents( "java.keystore.file");
//     echo "keyFile -->$keyFile" . "\n";
    $certFile = file_get_contents( "cert.pem");
//     echo "certFile -->$certFile" . "\n";
    $keyPass="33333";//key to decrypt keystore file
       echo "keyPass -->$keyPass" . "\n";
    //Start Curl process
    $handle=curl_init();
    if(FALSE==$handle)
       echo "Unable to create Url Object" . "\n";
    else
       echo "Able to create Url Object" . "\n";

    $val=curl_setopt($handle,CURLOPT_URL,$url); 
        if(false==$val)
          echo "Error 1";
    $val=curl_setopt($handle,CURLOPT_HTTPHEADER,$headers);   
        if(false==$val)
          echo "Error 2";
    $val=curl_setopt($handle,CURLOPT_RETURNTRANSFER,true);   
        if(false==$val)
          echo "Error 2";
    $val=curl_setopt($handle,CURLOPT_POST, true);   
        if(false==$val)
          echo "Error 4";
    $val=curl_setopt($handle, CURLOPT_POSTFIELDS, $credentials); 
        if(false==$val)
          echo "Error 5";
    $val=curl_setopt($handle, CURLOPT_SSLKEY, $keyFile);
        if(false==$val)
          echo "Error 6";
//    curl_setopt($handle, CURLOPT_SSLKEYPASSWD, $keyPass);
     $val=curl_setopt($handle, CURLOPT_SSLCERT, $certFile);
        if(false==$val)
          echo "Error 7";
    $val= curl_setopt($handle, CURLOPT_SSLCERTPASSWD, $keyPass);
        if(false==$val)
          echo "Error 8";
     //  echo "handle -->$handle" . "\n";
    print_r($handle);

    $response=curl_exec($handle);
       echo "response -->$response" . "\n";

    $status_code=curl_getinfo($handle, CURLINFO_HTTP_CODE);
       echo "status code --> $status_code" . "\n";
       curl_close($handle); 

?>

在上面的代码中,我需要设置自定义标题。 但是我收到 $response 为 null 和 $status_code 为 0。请告诉我代码有什么问题。我拥有的密钥库文件用于 java 和 andriod。根据操作系统,我们是否需要任何特定类型的文件。我正在 Windows 7 + Wamp(Apache2) 上尝试它,但是一旦在本地完成测试,就需要在 Linux 2.6.32-38-server 上使用 Apache2。

请推荐....

您好,我可以使用以下代码向我的服务器发出请求

$headers=array('contentType:application/json','MY-API-Key:ASDASDADADADASDASDASDASDASDASD','Accept-Language:null');                     

    curl_setopt_array($handle, array(
    CURLOPT_SSL_VERIFYPEER => true,
    CURLOPT_SSL_VERIFYHOST => 2,
    CURLOPT_VERBOSE => true,
    CURLOPT_HEADER => true,
    CURLOPT_CAINFO => 'C:\wamp\www\my.pem',
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_USERPWD => base64_encode("uname:pswd"),
    //CURLOPT_SSLCERT => 'C:\wamp\www\my.pem',
    //CURLOPT_SSLKEY => 'C:\wamp\www\my-publickey.keystore',
    CURLOPT_SSLCERTPASSWD => $keyPass,
    CURLOPT_SSLKEYPASSWD => $keyPass,
    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
    CURLOPT_RETURNTRANSFER => false,
              But when I sent this request to server, it responds back with        HTTP/1.1 500 Internal Server Error Server: Apache-Coyote/1.1 Content-Type: application/json Content-Length: 35 Date: Sat, 21 Dec 2013 13:39:22 GMT Connection: close {"errorCode":"0","errorMessage":""}success while loading page: 1header_size-->173 header --->1 body---"":                                If I make this request from RestClient then I am getting proper response                                                                    



   Status Code: 200 OK
    Content-Encoding: gzip
    Content-Length: 134
    Content-Type: application/json
    Date: Sat, 21 Dec 2013 13:27:51 GMT
    Server: Apache-Coyote/1.1

我想知道我需要设置什么才能获得正确的响应。我正在正确获取一些参数,如下所示。
例如。服务器:,内容类型:,内容长度:日期:
那么其他参数没有出现的原因可能是什么......一些输入会被应用......

【问题讨论】:

    标签: php apache curl


    【解决方案1】:

    我认为你应该添加两个 cURL 选项,然后看看会发生什么:

    curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
    

    【讨论】:

      【解决方案2】:

      This link 表示您必须提供证书的文件路径。不是文件的内容。

      Curl's setopt manual 也表示其 [CURLOPT_SSLKEY -- 包含私有 SSL 密钥的文件的名称]

      【讨论】:

      • 我已经改进了问题并添加了我的输入。请参见上文。
      • 如果你使用 CURLOPT_RETURNTRANSFER => true,它会返回响应内容。
      猜你喜欢
      • 1970-01-01
      • 2011-03-24
      • 2014-05-27
      • 1970-01-01
      • 2011-12-13
      • 2012-07-06
      • 1970-01-01
      • 2018-01-07
      • 2013-04-09
      相关资源
      最近更新 更多