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

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.

  • 写回答

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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度