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)));