空请求通过Curl发送,提供500内部服务器错误

时间:2014-01-06 12:39:30

标签: php apache curl

我已经使用curl_setopt_array函数通过curl设置参数但是当我打印我设置的选项时,例如然后它显示[header_size] => 0和[request_size] => 0 PFB代码片段。

$url='https://mysite.com:443/login';
$headers=array('contentType:application/json','X-API-Key:34SDFSDFvvsdfsdEERER45');
$keyPass="passphrase";

    $ret=curl_setopt_array($handle, array(
    CURLOPT_SSL_VERIFYPEER => TRUE,
    CURLOPT_SSL_VERIFYHOST => 2,
    CURLOPT_VERBOSE => TRUE,
    CURLOPT_HEADER => TRUE,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_CAINFO => 'C:\wamp\www\server.pem',
    //CURLOPT_USERPWD => 'uid=>username,password=>pwd',
    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/20100101 Firefox/26.0",
    CURLOPT_POST => TRUE,
    CURLOPT_POSTFIELDS => array("uid"=>'username',"password"=>'pwd'),
    CURLOPT_FAILONERROR => TRUE,
    ));
}

if (false==$ret) {
  echo "Unable to set curl options";
}
    $info=curl_getinfo($handle);
    print_R($info); 

    $response=curl_exec($handle);

    $info=curl_getinfo($handle);
    print_R($info); 

Output after execution

<html>
    <title>Consume Rest With Curl</title>
    </head>
    <body>
    Array //request to server
    (
        [url] => https://mysite.com:443/login
        [content_type] => 
        [http_code] => 0
        [header_size] => 0
        [request_size] => 0
        [filetime] => 0
        [ssl_verify_result] => 0
        [redirect_count] => 0
        [total_time] => 0
        [namelookup_time] => 0
        [connect_time] => 0
        [pretransfer_time] => 0
        [size_upload] => 0
        [size_download] => 0
        [speed_download] => 0
        [speed_upload] => 0
        [download_content_length] => -1
        [upload_content_length] => -1
        [starttransfer_time] => 0
        [redirect_time] => 0
        [certinfo] => Array
            (
            )

        [primary_ip] => 
        [primary_port] => 0
        [local_ip] => 
        [local_port] => 0
        [redirect_url] => 
    )
    Array //response from server
    (
        [url] => https://mysite.com:443/login
        [content_type] => 
        [http_code] => 500
        [header_size] => 25
        [request_size] => 362
        [filetime] => -1
        [ssl_verify_result] => 0
        [redirect_count] => 0
        [total_time] => 0.265
        [namelookup_time] => 0
        [connect_time] => 0.031
        [pretransfer_time] => 0.14
        [size_upload] => 248
        [size_download] => 0
        [speed_download] => 0
        [speed_upload] => 935
        [download_content_length] => -1
        [upload_content_length] => 248
        [starttransfer_time] => 0.187
        [redirect_time] => 0
        [certinfo] => Array
            (
            )

        [primary_ip] => server ip
        [primary_port] => 443
        [local_ip] => client ip
        [local_port] => 50909
        [redirect_url] => 
    )
    Error --> The requested URL returned error: 500 Internal Server Error
    </body>
    </html>

我想知道当我们发出请求时,数组shd中的所有参数都会显示一些值(例如标题大小,请求大小)。如果我错了或代码有问题,请纠正我。 我在服务器端验证了这一点,似乎标头已到达服务器但其他请求参数为空。我只是在Post Fields中传递用户名和密码,即正文。

Regds

4 个答案:

答案 0 :(得分:1)

实际上更改这些参数..

 CURLOPT_SSL_VERIFYPEER => FALSE,
    CURLOPT_SSL_VERIFYHOST => FALSE,

答案 1 :(得分:0)

像这样更改您的代码

CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE,

因为您处理HTTPS服务器所以这个内容非常安全。所以你必须转向ssl

<强>更新

您需要添加http标头Accept: application/json';

curl_setopt($ch1, CURLOPT_POSTFIELDS, $post_string1); //Send the data to the file
curl_setopt($ch1, CURLOPT_POST, true);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch1, CURLOPT_URL, $path1 );
curl_setopt($ch1, CURLOPT_HTTPHEADER, array('Accept: application/json')); 
curl_setopt( $ch1, CURLOPT_HTTPHEADER, $header );
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

答案 2 :(得分:0)

在分析请求后发现我们发送了错误的标头(contentType而不是Content-Type),并且在curl的post字段中,我们以非json格式发送用户名和密码。所以我们以json格式发送它并在发送之前使用json_encode对其进行编码。

感谢所有帮助解决此问题的人。

Regds

答案 3 :(得分:0)

尝试此方法以获取解决方案。

function callThirdPartyPostAPI( $url,$postField ,$method = 'get'  )
    {
        $ch1 = curl_init();

    if(  strtolower($method)  == 'get')
     {
         curl_setopt($ch1,CURLOPT_URL,$url);
         curl_setopt($ch1,CURLOPT_RETURNTRANSFER,true);
         curl_setopt($ch1, CURLOPT_HTTPHEADER, array('Accept: application/json'));
     }
     elseif(   strtolower($method)  == 'post' )
     {
         curl_setopt($ch1,CURLOPT_URL,$url);
         curl_setopt($ch1,CURLOPT_RETURNTRANSFER,true);
         curl_setopt($ch1,CURLOPT_HEADER, false);
         curl_setopt($ch1, CURLOPT_HTTPHEADER, array('Accept: application/json'));
         curl_setopt($ch1, CURLOPT_POST, 1);
         curl_setopt($ch1, CURLOPT_POSTFIELDS, json_encode($postField,true));
     }

        $response = json_decode(curl_exec($ch1),true);
        $err = curl_error($ch1);
        curl_close($ch1);

        if ($err) {
            return ["status"=>"cURL Error #:" . $err];
        } else {
            return $response;
        }
    }

我在get方法中这样调用。

https://www.XXXX.in/XXX/xxx/onxxxxn/revert-back-admission?id=3&h=23jj3gghj131