无法在php curl中设置证书和密钥库详细信息

时间:2013-12-20 08:32:20

标签: php apache curl

我在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)上尝试它,但是一旦测试在本地完成,那么需要在Apache 2.6.32-38服务器上使用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

我想知道在我的目的中需要设置什么来获得这个正确的答案。我正确地得到了一些参数如下 例如。服务器:,内容类型:,内容长度:日期:
那么可能是其他参数没有出现的原因......一些投入会被扼要......

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

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