无法使用Guzzle 6.x从Twilio获取api数据

时间:2019-01-02 11:09:45

标签: php json codeigniter twilio guzzle

我正在尝试检索已发送的传真列表。根据Twilio上的文档,我可以在https://fax.twilio.com/v1/Faxes处获得列表(我当然必须提供authtoken和sid)。我现在正在尝试使用Guzzle 6.x复制相同的内容。这是我的代码(我正在使用codeigniter 3.x):

// fax history
function fax_history(){

    // get the account sid                    
     $mh_twilio_fax_account_sid  = $this->config->item('mh_twilio_fax_account_sid');

    // get the auth token for the account
     $mh_twilio_auth_token       = $this->config->item('mh_twilio_auth_token');

    // create a new guzzle client
    $guzzleResource = new guzzleClient();

    // guzzle up the data
    $res = $guzzleResource->request('GET', 'https://fax.twilio.com/v1/Faxes', [
        'auth' => [$mh_twilio_fax_account_sid, $mh_twilio_auth_token]
    ]);

    // test to see if the right data is returned
    $this->data['status_code']   = $res->getStatusCode();
    $this->data['content_type']  = $res->getHeaderLine('content-type');
    $this->data['faxes']        = $res->getBody();

    // Set the titles
    $this->data['mh_admin_title']      = $this->data['mh_title_admin'] .' | Fax List - '.$this->module_view_name;

    // nominate the view file
    $this->data['mh_admin_view_file']   = $this->module_system_name .'/admin/'.$this->module_system_name.'-admin-fax-history';

    // load up the view
    $this->load->view($this->mh_template, $this->data);
}

代码运行后,我在$ this-> data ['status_code'],$ this-> data ['content_type']和$ this-> data ['faxes']的视图文件中看到以下内容:

$ this-> data ['status_code']说200 $ this-> data ['content_type']表示application / json $ this-> data ['faxes']没有提供我可以在https://fax.twilio.com/v1/Faxes看到的传真列表。而是显示:

GuzzleHttp\Psr7\Stream Object (
 [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #47
 [size:GuzzleHttp\Psr7\Stream:private] => 
 [seekable:GuzzleHttp\Psr7\Stream:private] => 1
 [readable:GuzzleHttp\Psr7\Stream:private] => 1
 [writable:GuzzleHttp\Psr7\Stream:private] => 1
 [uri:GuzzleHttp\Psr7\Stream:private] => php://temp
 [customMetadata:GuzzleHttp\Psr7\Stream:private] => Array
    (
    )

)

我如何获取看起来像这样的传真信息:

"meta": {"page": 0, "page_size": 50, "first_page_url": 
"https://fax.twilio.com/v1/Faxes?PageSize=50&Page=0", "previous_page_url": 
null, "url": "https://fax.twilio.com/v1/Faxes?PageSize=50&Page=0", 
"next_page_url": null, "key": "faxes"}, "faxes": [{"media_sid": 
"*snip*", "status": "delivered", "direction": "outbound", "from": 
"+81removed", "date_updated": "2019-01-02T08:16:43Z", "price": "-21.6", 
"account_sid": "*snip*", "to": "+81removed", "date_created": "2019-01- 
02T08:13:15Z", "url": 
"https://fax.twilio.com/v1/Faxes/FXe588329bf71f3f411da742d84838a25a", "sid": 
"*snip*", "duration": 193, "num_pages": 1, "quality": "fine", "price_unit": 
"JPY", "api_version": "v1", "media_url": 
"https://media.twiliocdn.com/fax/*snip*/*snip?x-amz-security-   etc etc

非常感谢任何指导。

1 个答案:

答案 0 :(得分:1)

食尸鬼6实现了psr7。正确访问内容的方法是:

$this->data['faxes'] = $res->getBody()->getContents();