简体   繁体   English

PHP CURL 在检索数据时出现 API 问题

[英]PHP CURL issue with an API when retrieving datas

we have an API existing with a distributor.我们有一个 API 与分销商一起存在。 Our software is running under PHP 7.3.6 / Apache 2.4.38 We already successfully did some other actions: creating new purchase orders, retrieving orders, ... We have a problem to retrieve invoices.我们的软件在 PHP 7.3.6 / Apache 2.4.38 下运行 我们已经成功地执行了一些其他操作:创建新的采购订单、检索订单……我们在检索发票时遇到了问题。 We are using for our API tests a software called POSTMAN.我们正在为我们的 API 测试使用名为 POSTMAN 的软件。 We input all the informations (api key, ....) Using POSTMAN, it works perfectly.我们输入所有信息(api 密钥,......)使用 POSTMAN,它完美地工作。 There is an option in POSTMAN to obtain the code in different langage. POSTMAN 中有一个选项可以获取不同语言的代码。 For our needs, we took the PHP generated code.为了我们的需要,我们采用了 PHP 生成的代码。 The problem is that it is not working.问题是它不起作用。 We also used https://reqbin.com/curl and it works perfectly.我们还使用了 https://reqbin.com/curl ,它运行良好。 But same problem, the generated code in PHP is not working.但同样的问题,PHP 中生成的代码不起作用。

For example, in postman or reqbin, this CURL code is working例如,在 postman 或 reqbin 中,此 CURL 代码有效

curl -X GET https://url.com/Invoices?startDate="01-01-2019"&endDate="01-31-2023" \
-H 'Accept: application/json' \
-H 'Authorization: Bearer generatedtokenzzzzzzzzzzzzzzzzzzzzzzzzzzz' \
-H 'Content-Type: application/json' \
-H 'zzzzzzzzz-API-Key: generatedapikeyzzzzzzzzzzzzzz'

when we click on generate PHP code we have this code:当我们点击生成 PHP 代码时,我们有以下代码:

<?php

$url = "https://url.com/Invoices?startDate="01-01-2019"&endDate="01-31-2023";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
   "Accept: application/json",
   "Authorization: Bearer generatedtokenzzzzzzzzzzzzzzzzzzzzzzzzzzz",
   "Content-Type: application/json",
   "zzzzzzzzz-API-Key: generatedapikeyzzzzzzzzzzzzzz",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);

?>

Because it didn't work, we tried to add some options like因为它不起作用,我们尝试添加一些选项,例如

CURLOPT_CUSTOMREQUEST => 'GET',

and it still don't work.它仍然不起作用。

We are getting crazy, we are on this problem for more than a week....我们快疯了,我们在这个问题上待了一个多星期......

Any help would be very usefull.任何帮助都会非常有用。

Many thanks by advance....非常感谢提前....

We find the bug.我们找到了错误。 it was not in the part of this code but it was above.它不在此代码的一部分,但在上面。 a parameter was not passed correctly, and the error message {message": "list index out of range} was in fact wrong.参数未正确传递,错误消息 {message": "list index out of range} 实际上是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM