CURL - [NULL响应]在Codeigniter中

时间:2017-02-07 06:42:33

标签: php curl codeigniter-3

我想从名为BirdEye的Third_party API中获取数据。我正在使用CURL的核心PHP内置函数来获取数据,它工作正常,现在当我切换到库时,我有点困惑,因为它没有给我任何响应作为回报。

我从这里下载了Curl Libray:Curl Library Download and Example

我试图创建一个演示只是为了检查我的图书馆是否正常工作,它有效。现在,如果我从Bird-Eye Api获取数据,我不知道它没有给我任何回应。 我的代码在这里:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {
public function index()
    {
        $this->load->library('curl');

//  Setting URL To Fetch Data From
$this->curl->create('https://api.birdeye.com/resources/v1/business/147802929307762?api_key=ApiKeyGoesHere');

//  To Temporarily Store Data Received From Server
$this->curl->option('buffersize', 10);

//  To support Different Browsers
$this->curl->option('useragent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 (.NET CLR 3.5.30729)');

//  To Receive Data Returned From Server
$this->curl->option('returntransfer', 1);

//  To follow The URL Provided For Website
$this->curl->option('followlocation', 1);

//  To Retrieve Server Related Data
$this->curl->option('HEADER', true);

//  To Set Time For Process Timeout
$this->curl->option('connecttimeout', 600);

//  To Execute 'option' Array Into cURL Library & Store Returned Data Into $data
$data = $this->curl->execute();
//  To Display Returned Data
echo '<pre>';
print_r($data);
        $this->load->view('welcome_message');
    }
}

我不知道我哪里出错我将所有必需的参数传递给Api。 链接到Api文档是:Link to BirdEye Api Documentation

1 个答案:

答案 0 :(得分:0)

我认为您在curl请求中缺少标题。因为根据api,响应将采用json格式。

    $this->load->library('curl');
    $get_url ="https://api.birdeye.com/resources/v1/customer/all?businessId=147802929307762&api_key=YOURAPIKEY"; 
    $result=$this->curl->simple_get($get_url, false, array(CURLOPT_USERAGENT => true, CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_HEADER => FALSE, CURLOPT_HTTPHEADER => array("Content-Type: application/json", "Accept: application/json"))); 
    $result=json_decode($result);
    echo '<pre>'; print_r($result);

在这里,我还通过卷曲请求传递了标题。

P.S。代码没有经过测试但它应该可以工作!!!!