tags:

views:

401

answers:

3

I'm trying to pass a username and password variable to the twitter credentials but it keeps returning that I'm not authenticated. However, when I use the actual username and password, instead of the variables, it successfully authorizes.

$username = $_POST["username"];
$password = $_POST["password"];

$url = "http://search.twitter.com/search.atom?q=golf&show_user=true&rpp=100";
$search = file_get_contents($url);

$regex_name = '/\<name\>(.+?) \(/';
preg_match_all($regex_name,$search,$user);
for($i=0;$user[1][$i];$i++)
{
$follow = $user[1][$i];
    define('TWITTER_CREDENTIALS', '$username:$password');
    $url = "http://twitter.com/friendships/create/".$follow.".xml";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_USERPWD, TWITTER_CREDENTIALS);
    $result= curl_exec ($ch);
    curl_close ($ch);

I'm thinking it has something to do with the colon in between the username and password, or perhaps trying to use variables within define function.

Any clues?

+1  A: 

it has something to do with using single quotes instead of double quotes.

SilentGhost
+6  A: 
$username = $_POST["username"];
$password = $_POST["password"];
// INCORRECT. Will literary assign TWITTER_CREDENTIALS as $username:$password
// define('TWITTER_CREDENTIALS', '$username:$password');

// CORRECT, will parse the variables and assign the result to TWITTER_CREDENTIALS
define('TWITTER_CREDENTIALS', "$username:$password");

Remember strings with double quotes (") parse variables in the string, strings with single quotes (') do not.

Read more about strings in PHP:

PatrikAkerstrand
A: 

You are using define inside a loop, that´s not gonna work because you can only define e constant once.

julioc
he's not using DEFINE properly, but that doesn't mean that it's not going to work. It'll give him a notice; but his username and password do not change anyway, so there's no big deal.
SilentGhost