我正在尝试将用户名和密码变量传递给twitter凭据,但它会一直返回,我没有通过身份验证.但是,当我使用实际的用户名和密码而不是变量时,它会成功授权.
$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);
Run Code Online (Sandbox Code Playgroud)
我认为它与用户名和密码之间的冒号有关,或者可能试图在define函数中使用变量.
有线索吗?
$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");
Run Code Online (Sandbox Code Playgroud)
请记住字符串中带双引号(")解析变量的字符串,带单引号的字符串(')不记住.
阅读PHP中有关字符串的更多信息