Reputation: 2762
I'm trying to use file_get_contents but it tells me failed to open stream.
My code:
$user="[email protected]";
$user_id=str_replace(array('@', '#'), array('%40', '%23'), $user);
print $user_id;
$url=('http://admin:[email protected]/@api/users/=$user_id/properties');
$xmlString=file_get_contents($url);
This is what I get when I try to run it:
Warning: file_get_contents(http://[email protected]/@api/deki/users/=$user_id/properties): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error
However, if I manually type in the $user_id first_last%40ourwiki.com then it works! What am I doing wrong? Shouldn't I be able to just use the variable name?
Remaining code:
$delete = "http://admin:[email protected]/@api/users/=$user_id/properties/%s";
$xml = new SimpleXMLElement($xmlString);
function curl_fetch($url,$username,$password,$method='DELETE')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_USERPWD,"$username:$password");
return curl_exec($ch);
}
foreach($xml->property as $property) {
$name = $property['name'];
$name2 =str_replace(array('@', '#'), array('%40', '%23'), $name);
print $name2;
curl_fetch(sprintf($delete, $name2),'admin','password');
}
Upvotes: 0
Views: 2121
Reputation: 13881
This is because you have used single quotes. The content within single quotes is not parsed, so:
echo '$test';
won't display the value of the $test
variable, but just the "$test" string. You can use double quotes instead, but anyway this is the best way to do it:
$url=('http://admin:[email protected]/@api/users/='.$user_id.'/properties');
Special characters such as \n
, \t
or \r
also won't be parsed in single quotes.
Upvotes: 0
Reputation: 23255
Variables contained in single-quoted strings are not interpreted. You could do this:
"http://admin:[email protected]/@api/users/=$user_id/properties"
But a better habit is to do this:
'http://admin:[email protected]/@api/users/=' . $user_id . '/properties'
or this:
"http://admin:[email protected]/@api/users/=" . $user_id . "/properties"
or this:
sprintf("http://admin:[email protected]/@api/users/=%s/properties", $user_id)
The faster is with single-quoted strings, because php doesn't try to find variables in them.
Upvotes: 2