PHP-无法打开URL包含变量的流

PHP-无法打开URL包含变量的流,php,Php,我试图使用文件获取内容,但它告诉我无法打开流 我的代码: $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_conten

我试图使用文件获取内容,但它告诉我无法打开流

我的代码:

$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);
这是我尝试运行它时得到的结果:

警告: 文件\u获取\u内容(http://...@172.16.214.133/@api/deki/users/=$user\u id/properties): 无法打开流:HTTP请求 失败!HTTP/1.1500内部服务器 错误

但是,如果我手动输入$user\u idfirst\u last%40ourwiki.com,它就会工作!我做错了什么?我不应该只使用变量名吗

剩余代码:

$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');
}

不解释单引号字符串中包含的变量。 您可以这样做:

"http://admin:password@172.16.214.133/@api/users/=$user_id/properties"
但更好的习惯是这样做:

'http://admin:password@172.16.214.133/@api/users/=' . $user_id . '/properties'
或者这个:

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

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

使用单引号字符串的速度更快,因为php不会尝试在字符串中查找变量。

这是因为您使用了单引号。单引号内的内容未被解析,因此:
echo'$test'
不会显示
$test
变量的值,只显示“$test”字符串。您可以使用双引号,但无论如何,这是最好的方法:

$url=('http://admin:password@172.16.214.133/@api/users/='.$user_id.'/properties');

特殊字符,如
\n
\t
\r
也不会在单引号中进行分析。

我只是将其改为双引号,看起来很有效。在另一个地方,我使用了user_id,它告诉我警告:sprintf()的参数太少,并且在我添加“%”时似乎是因为“%”才起作用。这正常吗?你知道有什么办法可以解决这个问题吗?我想我会问:)我真的不知道你在说什么。请发布一些代码让我知道更多;)对不起,我添加了剩余的代码。如果我使用double%%将$user_id手动输入$delete字符串,则此操作正常。否则它会给我警告:sprintf():它找到的每个属性的参数太少。似乎有点奇怪,对不起,我不知道如何解决这个问题——我也被它弄糊涂了;可能的副本