Twilio cURL请求只返回一个信息字符串



我正试图在php函数中使用cURL来获取所有Twilio编号的列表。我正在从Twilio获得我需要的信息,但它只是一个没有空格或任何东西的所有信息的字符串。

这就是我要得到的。我已经删除了我的账号和sid:

string(1621) " {{ SID }}ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX(936) 585-6544+19365856544https://demo.twilio.com/welcome/sms/reply/POSTPOSTfalseWed, 11 May 2016 14:39:22 +0000Wed, 18 May 2016 15:41:25 +0000https://demo.twilio.com/welcome/sms/reply/POSTPOSTnonefalsetruetruetruePOST2010-04-01/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/IncomingPhoneNumbers/PN5e0ef24464a1fbbcf4e2bd127c471892"

这是我用来cURL Twilio的函数。

function post_incoming($POST) {
    // resource url & authentication
    $uri = 'https://api.twilio.com/2010-04-01/Accounts/' . $this->sid . '/IncomingPhoneNumbers';
    $auth = $this->sid . ':' . $this->authtoken;
    $res = curl_init();
    // set cURL options
    curl_setopt( $res, CURLOPT_URL, $uri );
    curl_setopt( $res, CURLOPT_USERPWD, $auth ); // authenticate
    curl_setopt( $res, CURLOPT_RETURNTRANSFER, true ); // don't echo
    // send cURL
    $result = curl_exec( $res );
    var_dump($result);
    return $result;
}

我需要以json数组或某种数组的形式返回信息,这样我就可以解析出Twilio的电话号码,以便稍后使用。

如果使用PHP帮助程序库,会容易得多。它看起来像这样:

// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "{{ auth_token }}";
$client = new Services_Twilio($sid, $token);
$numbers_arr = array();
// Loop over the list of numbers and echo a property for each one
foreach ($client->account->incoming_phone_numbers as $number) {
    $numbers_arr[] = $number->phone_number;
}
return json_encode($numbers_arr);

}

此函数使用Twilio PHP SDK获取帐户中所有数字的列表,然后将它们放入数组变量$numbers_arr中,并将该变量作为JSON返回。

最新更新