51Testing软件测试论坛

标题: PHP cURL给出错误但在POSTMAN中工作正常 [打印本页]

作者: 测试积点老人    时间: 2022-7-28 13:15
标题: 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 :
  1. <?php

  2. $curl = curl_init();

  3. curl_setopt_array($curl, array(
  4.   CURLOPT_URL => "https://www.example.com/dos/AW/API",
  5.   CURLOPT_RETURNTRANSFER => true,
  6.   CURLOPT_ENCODING => "",
  7.   CURLOPT_MAXREDIRS => 10,
  8.   CURLOPT_TIMEOUT => 30,
  9.   CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  10.   CURLOPT_CUSTOMREQUEST => "POST",
  11.   CURLOPT_POSTFIELDS => "{\"Filter\" : {\"IsActive\" : \"True\",\"OutputSelector\" : \"Name\"}}",
  12.   CURLOPT_HTTPHEADER => array(
  13.     "API_ACTION: GetItem",
  14.     "API_KEY: MHlIARzQqxVpOg2dUxH4q9w7bx3pOL6K",
  15.     "Accept: application/json"
  16.   ),
  17. ));

  18. $response = curl_exec($curl);
  19. $err = curl_error($curl);

  20. curl_close($curl);

  21. if ($err) {
  22.   echo "cURL Error #:" . $err;
  23. } else {
  24.   echo $response;
  25. }

  26. ?>
复制代码
  1. 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 "?

  2. I would appreciate if someone could help me with this. Thank you very much.
复制代码



作者: qqq911    时间: 2022-7-29 11:01
比对下两个工具的请求,包含请求头
作者: bellas    时间: 2022-7-29 14:23
参照一楼
作者: jingzizx    时间: 2022-7-29 16:35
看报文




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2