0

I'm using shell_exec() to execute a Twitter API Call.

shell_exec('curl -u user:password -d "id=3191321" http://api.twitter.com/1/twitterapi/twitterlist/members.xml'); 

That works fine when I authenticate correctly and put in a number for the id.

But when I try to put in a variable ($id), it screws up.

$addtolist = shell_exec('curl -u user:pw -d "id='.$id.'" http://twitter.com/username/twitterlist/members.xml'); 

I tried flipping the quote types

$addtolist = shell_exec("curl -u user:pw -d 'id=$id' http://twitter.com/username/twitterlist/members.xml"); 

I tried using double quotes and escaping them

$addtolist = shell_exec("curl -u user:pw -d \"id=$id\" http://twitter.com/username/twitterlist/members.xml"); 

None of them worked.

What am I doing wrong?

EDIT: The purists say I should be using PHP's built in curl methods, not the shell_exec. That's not working either.

$url = 'http://twitter.com/user/list/members.xml';

// Set up and execute the curl process

$curl_handle = curl_init();

curl_setopt($curl_handle, CURLOPT_URL, "$url");

curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);

curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl_handle, CURLOPT_POST, 1);

curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "id=$id");

curl_setopt($curl_handle, CURLOPT_USERPWD, "user:pw");

$buffer = curl_exec($curl_handle);

curl_close($curl_handle);

It returns bool(false), and doesn't properly update the Twitter List in question (the whole point of the exercise)

6
  • Out of curiosity, why are you using shell_exec (which implies forking another process, depending on an external application, and might not work that well on windows), and not directly using the curl extension (see fr.php.net/curl ) ? Commented Apr 1, 2010 at 17:54
  • Ironically, for the sake of simplicity. exec() has the same problem. cURL() is messy, and I'm basically just taking the example directly out of the Twitter API wiki. Is there to way to pass a variable into the command line like this, though? Commented Apr 1, 2010 at 18:03
  • I agree, I'm not gonna help you do something a horribly incorrect way. Use the PHP CURL extension, it's not that complicated. Or use one of the many PHP Twitter libraries. (I counted 12 in a quick search) Commented Apr 1, 2010 at 18:28
  • This is a call to Twitter Lists, which most of the libraries don't have, since it's a new API. Commented Apr 1, 2010 at 18:31
  • CURL is not working either. See the above edit. Commented Apr 1, 2010 at 19:45

3 Answers 3

2

Try printing the string echo "curl -u pxlist:Weekend1 -d 'id=$id' http://twitter.com/username/twitterlist/members.xml"; and see what it says. Probably there's something wrong with $id. How is it initialized?

Sign up to request clarification or add additional context in comments.

1 Comment

It looks like you're right about $id's initialization. Working on fixing it.
0

Have you tried :

shell_exec("curl -u user:password -d \"id=" .$id. "\" http://api.twitter.com/1/twitterapi/twitterlist/members.xml"); 

and as Filip suggested are you sure that $id is initialized, try echoing before executing .. try this above if $id is initialized

1 Comment

$id is initialized. This doesn't work either. (Why did you insert the 3191321?)
0

use escapeshellarg() to properly encode $id for a command line argument.

$addtolist = shell_exec('curl -u user:pw -d '.escapeshellarg("id=$id").' http://twitter.com/username/twitterlist/members.xml');

(i realize this question is old, 8 years old as of writing, but nobody provided the correct solution.)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.