douqie3391 2015-07-30 02:09
浏览 46
已采纳

Twilio Media - 在非对象上调用成员函数get() - Twilio帮助程序库被破坏了吗?

Pretty much trying to do something exactly as defined in the examples on the Twilio REST API website.

https://www.twilio.com/docs/api/rest/media

In the examples, the following code deletes media in the account library.

$client->account->messages->get("MM800f449d0399ed014aae2bcc0cc2f2ec")->media->delete("ME557ce644e5ab84fa21cc21112e22c485");

and this code is supposed to return a list of the media assets associated with a specific message:

foreach ($client->account->messages->get('MM800f449d0399ed014aae2bcc0cc2f2ec')->media as $media) {
     echo $media->content_type;
 }

The problem is both of those examples return the error:

Call to a member function get() on a non-object

I'm not a PHP expert, and I've tried debugging this to the best of my capabilities. I am using the exact same call to $client->account->messages elsewhere in the script successfully, so I know that there is an object there, and can only assume the get() method is broken somehow.

I've read through the following but non seem to answer my issue:
Reference - What does this error mean in PHP?
PHP Error: "Call to member function create() on a non-object" Twilio code
calling to a member function create() on a non-object

Anyone with Twilio experience familiar with the issue here? Any way to at least confirm whether this is a PHP error on my part or is something wrong with the Twilio Media API, or the example code? Any and all help is appreciated.

  • 写回答

1条回答 默认 最新

  • duanlin1931 2015-07-31 17:49
    关注

    The PHP error is still a mystery, since there is an object at $client->account->messages.

    That said, I was able to interact with the media in my Twilio account by directly using CURL requests rather than using their helper library, which does appear to be where the get function is broken (can't confirm this and nobody here or at Twilio has been any help clearing that up - no time to dig through their library to try to diagnose it).

    This article pointed out how to do the CURL interaction: How do I make a request using HTTP basic authentication with PHP curl?

    As stated above, you can use the Twilio helper library function $client->account->messages to get all the messages from your account. That said, as far as I can tell, the get function described in several of their How To articles doesn't work, so you can't pull individual messages without knowing the URL.

    $messages = $client->account->messages;
    foreach($messages as $message) {
         #code here to get and use the data from your message list
    }
    

    That allowed me to get the URLs of the media assets that had been sent to me in messages, but it still didn't allow me to delete media from my account to save on storage space. In order to do that I had to do the following (the $url is the url of the media asset I want to delete, the $sid and $token are my Twilio account SID and Auth Token):

    function deleteMedia($url,$sid,$token) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_USERPWD, $sid.":".$token);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $output = curl_exec($ch);
        curl_close($ch);
    }
    

    Probably not the most elegant solution, but it works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog