dongshu7162
2017-07-21 09:09
浏览 1.5k
已采纳

PHP cURL给出错误但在POSTMAN中工作正常

I have some problem(s) with PHP cURL. I tried to get data from the API using PHP cURL. This is my cURL code in PHP :

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://www.example.com/dos/AW/API",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"Filter\" : {\"IsActive\" : \"True\",\"OutputSelector\" : \"Name\"}}",
  CURLOPT_HTTPHEADER => array(
    "API_ACTION: GetItem",
    "API_KEY: MHlIARzQqxVpOg2dUxH4q9w7bx3pOL6K",
    "Accept: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

?>

With that code I can get a response but the response contains some errors. I also tried using POSTMAN to check, and the API works fine as I got a successful response with the same data. My question is: "Is there anything wrong with my cURL code that would explain why I got an error when I used cURL and I got successful response in POSTMAN "?

I would appreciate if someone could help me with this. Thank you very much.

图片转代码服务由CSDN问答提供 功能建议

我有一些PHP cURL的问题。 我试图使用PHP cURL从API获取数据。 这是我在PHP中的cURL代码:

 &lt;?php 
 
 $ curl = curl_init(); 
 
 ncurl_setopt_array($ curl,array(
 CURLOPT_URL)  =&gt;“https://www.example.com/dos/AW/API",
 CURLOPT_RETURNTRANSFER =&gt; true,
 CURLOPT_ENCODING =&gt;”“,
 CURLOPT_MAXREDIRS =&gt; 10,
 CURLOPT_TIMEOUT =  &gt; 30,
 CURLOPT_HTTP_VERSION =&gt; CURL_HTTP_VERSION_1_1,
 CURLOPT_CUSTOMREQUEST =&gt;“POST”,
 CURLOPT_POSTFIELDS =&gt;“{\”过滤器\“:{\”IsActive \“:\”True \“,\  “OutputSelector \”:\“Name \”}}“,
 CURLOPT_HTTPHEADER =&gt;数组(
”API_ACTION:GetItem“,
”API_KEY:MHlIARzQqxVpOg2dUxH4q9w7bx3pOL6K“,
”接受:application / json“
)  ,
)); 
 
 $ response = curl_exec($ curl); 
 $ err = curl_error($ curl); 
 
 ncurl_close($ curl); 
 
if($ err){
  echo“cURL Error#:”。  $ err; 
} else {
 echo $ response; 
} 
 
?&gt; 
   
 
 

使用该代码我可以得到回复但是 响应包含一些错误。 我也尝试使用 POSTMAN 进行检查,并且API工作正常,因为我使用相同的数据得到了成功的响应。 我的问题是:“我的 cURL 代码有什么问题可以解释为什么我在使用 cURL 时遇到错误,并且我在 POSTMAN <中得到了成功的响应 / code>“?

如果有人能帮助我,我将不胜感激。 非常感谢你。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douyuan1049 2017-07-21 11:17
    已采纳

    given that you aren't showing us the successful postman request, we can't know for sure what errors you make, that said, you are making a couple of obvious mistakes here.

    first off, when debugging curl code, use CURLOPT_VERBOSE , it gies you a lot of useful information when debugging your curl requests (and if you did this, you would probably notice how the Postman requests's content-type is completely different from curl's content-type http headers - more on this soon)

    second, when you want a POST request, don't use CURLOPT_CUSTOMREQUEST, use CURLOPT_POST.

    third, when passing a string to CURLOPT_POSTFIELDS, the content-type implicitly becomes Content-Type: application/x-www-urlencoded, unless you override it. and you are obviously NOT sending x-www-urlencoded data, but JSON-encoded data, so your content-type is all wrong, its supposed to be Content-type: application/json

    fourth, you can hardcode the json if you want, but the code looks much prettier if you json_encode it

    fifth, don't use setopt / setopt_array without checking the return type.

    fixing all that, you'll end up with something like:

    function ecurl_setopt_array($ch, array $options) {
        if (! curl_setopt_array ( $ch, $options )) {
            throw new \RuntimeException ( 'curl_setopt_array failed. ' . curl_errno ( $ch ) . ': ' . curl_error ( $ch ) );
        }
    }
    
    $curl = curl_init ();
    
    ecurl_setopt_array ( $curl, array (
            CURLOPT_URL => "https://www.example.com/dos/AW/API",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_VERBOSE => true,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => json_encode ( array (
                    'Filter' => array (
                            'IsActive' => 'True',
                            'OutputSelector' => 'Name' 
                    ) 
            ) ),
            CURLOPT_HTTPHEADER => array (
                    "API_ACTION: GetItem",
                    "API_KEY: MHlIARzQqxVpOg2dUxH4q9w7bx3pOL6K",
                    "Accept: application/json",
                    'Content-Type: application/json' 
            ) 
    ) );
    
    $response = curl_exec ( $curl );
    $err = curl_error ( $curl );
    
    curl_close ( $curl );
    
    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo $response;
    }
    

    Edit: fixed the json data, when i wrote it, i didn't see that isActive is not an actual boolean, but the string literal True - i mistakenly encoded it as a json boolean true instead, sorry, fixed. (although i suspect it's supposed to be a boolean anyway, and that your original code just encodes it wrong, perhaps you should double check isActive's type in the api docs, assuming there is one)

    点赞 打赏 评论
  • dqoag62688 2017-07-21 09:47

    @Antonio, Response you are getting is from the other end, might be you are missing something which restrict the processing of query at other end. try to print http_code, or use curl_getinfo to get complete information.

    in case of response code is 200, then you may ask from another end to validate the request.

    PS: not able to comment because of repo restrictions.

    点赞 打赏 评论

相关推荐 更多相似问题