Tutorial :Twitter API Error using variables within php DEFINE


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?


$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:


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


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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »