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 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置