0

I'm trying to use file_get_contents but it tells me failed to open stream.

My code:

$user="first_last@ourwiki.com";
$user_id=str_replace(array('@', '#'), array('%40', '%23'), $user);
print $user_id;

$url=('http://admin:password@172.16.214.133/@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://...@172.16.214.133/@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:password@172.16.214.133/@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');
}
Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76
Aaron
  • 2,672
  • 10
  • 28
  • 45
  • Possible duplicate of [Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Sep 10 '16 at 10:37

2 Answers2

2

Variables contained in single-quoted strings are not interpreted. You could do this:

"http://admin:password@172.16.214.133/@api/users/=$user_id/properties"

But a better habit is to do this:

'http://admin:password@172.16.214.133/@api/users/=' . $user_id . '/properties'

or this:

"http://admin:password@172.16.214.133/@api/users/=" . $user_id . "/properties"

or this:

sprintf("http://admin:password@172.16.214.133/@api/users/=%s/properties", $user_id)

The faster is with single-quoted strings, because php doesn't try to find variables in them.

greg0ire
  • 22,714
  • 16
  • 72
  • 101
0

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:password@172.16.214.133/@api/users/='.$user_id.'/properties');

Special characters such as \n, \t or \r also won't be parsed in single quotes.

rhino
  • 13,543
  • 9
  • 37
  • 39
  • I just changed it to double quotes and looks like its working. I have another place where I use the user_id and it tells me warning:sprintf() too few arguments and appears that it's because of '%' when I add '%%' it works. Is this normal? You know of anyway to get around this? Just thought i'd ask :) – Aaron Oct 02 '10 at 17:37
  • I don't really know what you're talking about. Please post some code to let me know more ;) – rhino Oct 02 '10 at 17:40
  • Sorry, I added the remaining code. If I manually enter the $user_id into the $delete string with double %% then this works fine. Otherwise it gives me Warning: sprintf(): Too few arguments for each property that it finds. Seems sort of strange – Aaron Oct 02 '10 at 17:48
  • I'm sorry, I don't know how to fix that - I am also confused by it ;s – rhino Oct 02 '10 at 18:24