Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:对文件\u get\u内容URL的请求不正确_Php_Url_File Get Contents_Urlencode - Fatal编程技术网

PHP:对文件\u get\u内容URL的请求不正确

PHP:对文件\u get\u内容URL的请求不正确,php,url,file-get-contents,urlencode,Php,Url,File Get Contents,Urlencode,这是我的密码: $url = "https://de.wikipedia.org/wiki/Creed_–_Rocky’s_Legacy"; $html = file_get_contents($url); 我得到了这个错误: HTTP request failed! HTTP/1.0 400 Bad Request 问题是URL中的特殊字符-如果我使用编码的URL(我可以从浏览器复制/粘贴),它会起作用 我尝试使用urlencode(),但它会对整个字符串进行编码,根本不起作用: http

这是我的密码:

$url = "https://de.wikipedia.org/wiki/Creed_–_Rocky’s_Legacy";
$html = file_get_contents($url);
我得到了这个错误:

HTTP request failed! HTTP/1.0 400 Bad Request
问题是URL中的特殊字符-如果我使用编码的URL(我可以从浏览器复制/粘贴),它会起作用

我尝试使用
urlencode()
,但它会对整个字符串进行编码,根本不起作用:

https%3A%2F%2Fde.wikipedia.org%2Fwiki%2FCreed_%96_Rocky%92s_Legacy

那么,如何对url上的特殊字符进行编码呢?

您只能对url的最后一部分进行编码

$url = "https://de.wikipedia.org/wiki/" . urlencode("Creed_–_Rocky’s_Legacy");
$html = file_get_contents($url);
或者,如果$url变量发生更改:

$url = "https://de.wikipedia.org/wiki/Creed_–_Rocky’s_Legacy";
$html = file_get_contents(dirname($url) . "/" . urlencode(basename($url)));

你的代码在这里工作。您是从cli还是http服务器运行?哪个php版本?另外,文件的编码和默认的php编码是什么?
$url = "https://de.wikipedia.org/wiki/Creed_–_Rocky’s_Legacy";
$html = file_get_contents(dirname($url) . "/" . urlencode(basename($url)));