php中的cUrL:没有任何反响[NULL]

时间:2017-02-03 11:32:31

标签: php json curl

我使用PHP中的CURL从第三方API获取数据。我已阅读文档并将相同的有效参数传递给请求但没有任何作用。 由于机密性,我正在显示删除“API KEY”的代码。

<?PHP include "./folder/Information.php"; ?>

它给我的输出是:$service_url = 'https://api.birdeye.com/resources/v1/business/147197756121167?api_key=ApiKeyGoesHere'; $curl = curl_init($service_url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $curl_response = curl_exec($curl); if ($curl_response === false) { $info = curl_getinfo($curl); curl_close($curl); echo '<pre>'; die('error occured during curl exec. Additioanl info: ' . var_export($info)); } curl_close($curl); $decoded1 = json_decode($curl_response,true); if (isset($decoded1->response->status) && $decoded1->response->status == 'ERROR') { die('error occured: ' . $decoded1->response->errormessage); } echo 'response ok!'; var_export($decoded1->response); ?>

链接到birdeye API的文档。

response ok!NULL

我尝试过使用终端进行测试,它给了我回复。 任何人都能给我路,我哪里出错了?

2 个答案:

答案 0 :(得分:0)

使用api密钥尝试此操作

 $urltopost = 'https://api.birdeye.com/resources/v1/business/147197756121167?api_key=ApiKeyGoesHere';
 $header=array("content-type"=>"application/json");
 $ch = curl_init ($urltopost);
 curl_setopt ($ch, CURLOPT_POST, true);
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt ($ch, CURLOPT_HTTPHEADER, $header);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 $returndata = curl_exec ($ch);
 $status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); 
 print_r($status_code);
 print_r($returndata);

答案 1 :(得分:0)

检查一下这段代码。这对我有用。

<?php
// Get cURL resource
$curl = curl_init();

// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => "YOUR API END POINT"

));

// Send the request & save response to $resp
$result = json_decode(curl_exec($curl),true);

// Close request to clear up some resources
curl_close($curl);

return $result;