【发布时间】: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
我想知道我需要设置什么才能获得正确的响应。我正在正确获取一些参数,如下所示。
例如。服务器:,内容类型:,内容长度:日期:
那么其他参数没有出现的原因可能是什么......一些输入会被应用......
【问题讨论】: