dongmaoluan5719 2010-10-02 17:27 采纳率: 100%
浏览 33
已采纳

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

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');
}
  • 写回答

2条回答 默认 最新

  • dowaw80220 2010-10-02 17:31
    关注

    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 , \t or also won't be parsed in single quotes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答