0

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.

Community
  • 1
  • 1
f71316
  • 252
  • 3
  • 13
  • no, the `get` method is not broken. PHP is telling you that at that point in the script, `$client->account->messages` is not an object. Have you tried dumping it to the screen to see what it is? Tracing back the last line where it's used that does work? – Jessica Jul 30 '15 at 03:07
  • `$client->account->messages` returns an object that contains my message list for the specified Twilio account, as expected from their documentation. The gt function appears to be missing. – f71316 Jul 30 '15 at 16:05
  • Read the error message. It's a basic PHP error. – Jessica Jul 30 '15 at 16:12

1 Answers1

1

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.

Community
  • 1
  • 1
f71316
  • 252
  • 3
  • 13