Reputation: 1563
I'm trying to retrieve data from an API and while one of my calls works fine, all the others end up with no output at all which makes things difficult for debugging.
I have stuffed my cURL request with many parameters like this:
curl_setopt($this->__ch, CURLOPT_URL, $apiUrl );
curl_setopt($this->__ch, CURLOPT_HEADER, 1);
curl_setopt($this->__ch, CURLOPT_USERAGENT, $_SERVER ['HTTP_USER_AGENT']);
curl_setopt($this->__ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->__ch, CURLOPT_NOBODY, 1);
curl_setopt($this->__ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($this->__ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($this->__ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->__ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->__ch, CURLOPT_VERBOSE, true);
curl_setopt($this->__ch, CURLOPT_VERBOSE, 1);
// only when access token is available
curl_setopt($this->__ch, CURLOPT_HTTPHEADER, "access-token:XXXX");
$result = curl_exec($this->__ch);
$info = curl_getinfo($this->__ch);
If I use the following content for $this, it works:
BanggoodAPI Object
(
[__apiKey:BanggoodAPI:private] => ******
[__apiSecret:BanggoodAPI:private] => ******
[__domain:BanggoodAPI:private] => https://affapi.banggood.com/
[__accessToken:BanggoodAPI:private] =>
[__task:BanggoodAPI:private] => getAccessToken
[__method:BanggoodAPI:private] => GET
[__params:BanggoodAPI:private] => Array
(
[api_key] => ******
[noncestr] => ******
[timestamp] => ******
[signature] => ******
)
[__lang:BanggoodAPI:private] => en-GB
[__currency:BanggoodAPI:private] => USD
[__waitingTaskInfo:BanggoodAPI:private] => Array
(
[task] => coupon/list
[method] => GET
[params] => Array
(
[category_id] => 0
[type] => 2
)
)
[__ch:BanggoodAPI:private] => Resource id #1
[__curlExpireTime:BanggoodAPI:private] => 10
)
I get a JSON in return which is the expected behaviour.
If I then use the following object, I get nothing at all in return:
BanggoodAPI Object
(
[__apiKey:BanggoodAPI:private] => ******
[__apiSecret:BanggoodAPI:private] => ******
[__domain:BanggoodAPI:private] => https://affapi.banggood.com/
[__accessToken:BanggoodAPI:private] => ******
[__task:BanggoodAPI:private] => coupon/list
[__method:BanggoodAPI:private] => GET
[__params:BanggoodAPI:private] => Array
(
[category_id] => 0
[type] => 2
[lang] => en-GB
[currency] => USD
)
[__lang:BanggoodAPI:private] => en-GB
[__currency:BanggoodAPI:private] => USD
[__waitingTaskInfo:BanggoodAPI:private] => Array
(
)
[__ch:BanggoodAPI:private] => Resource id #1
[__curlExpireTime:BanggoodAPI:private] => 10
)
If I try to access directly the cURL destination in my browser and I add the access token in the header, it works perfectly fine which means that the endpoint is working and there is a problem with my PHP.
For this second object, $result is empty, $info is empty too. It's like the request was completely ignored. The endpoint is suppose to give me a json in all cases (ex: to tell me that there is an error).
Do you have any idea that would explain why the output is empty ? Or something that would force PHP to give me more information about what is happening?
Thanks!
Upvotes: 0
Views: 227
Reputation: 81
Consider using endpoint tester like postman https://www.postman.com. That should tell you what is wrong with request.
Furthermore you have option CURLOPT_NOBODY to 1, try without it. Also you can remove redundant CURLOPT_SSL_VERIFYPEER and CURLOPT_VERBOSE. In curl documentation you have description about every curl option https://www.php.net/manual/en/book.curl
Upvotes: 1