解决方案,因为objective不编码&(因为它是保留的),您应该创建自己的方法。来源:Sending an amp (&) using a post with Obj-C
对于我的iOS和安卓应用程序,我使用一个php脚本来获取一些数据。这个脚本有一个参数,这是一个链接。基本上,这个脚本看起来如下:
$link= urlVariable('link');
$source = file_get_contents($link);
$xml = new SimpleXMLElement($source);
//Do stuff with the xml但是当我发送带有&符号的链接时,它会在file_get_contents上崩溃
Warning: file_get_contents(https://www.example.com/xxxx/name_more_names_) [function.file-get-contents]: failed to open stream: HTTP request failed!但完整的论点是
name_more_names_&_more_names_after_the_ampersand.file在将链接发送到file_get_contents之前,我尝试过对其进行编码,但没有结果。
也曾尝试过:
function file_get_contents_utf8($fn) {
$content = file_get_contents($fn);
return mb_convert_encoding($content, 'UTF-8',
mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
}结果是一样的。
有人知道它为什么会中断吗?我不是web开发人员,而是一个应用程序开发人员,所以请原谅我对这个问题缺乏了解。
编辑也尝试过这样做:
$link= urlVariable('link');
$encodedLink = urlencode($link);
$source = file_get_contents($encodedLink);其结果是:
Warning: file_get_contents(https%3A%2F%2Fwww.example.com%2Fxxxxx%2Fname_more_names_) [function.file-get-contents]: failed to open stream: No such file or directory in编辑#2
我找到了为什么我的网址停留在&符号。我使用以下方法从url检索我的论点:
function urlVariable($pVariableName)
{
if (isset ($_GET[$pVariableName]))
{
$lVariable = $_GET[$pVariableName];
}
else
{
$lVariable = null;
}
return $lVariable;
}_GET()用&符号分隔参数,对吗?
发布于 2013-04-05 08:33:13
URL应该有正确编码的符号:
https://www.example.com/xxxx/name_more_names_%26_more_names_after_the_ampersand.file在PHP中,您可以对路径进行如下编码:
$path = parse_url($url, PHP_URL_PATH);
$url = substr_replace($url, '/' . urlencode(substr($path, 1)), strpos($url, $path), strlen($path));更新
如果还具有树结构,则需要做更多的工作:
$path = parse_url($url, PHP_URL_PATH);
$newpath = '/' . str_replace('%2F', '/', urlencode(substr($path, 1)));
$url = substr_replace($url, $newpath, strpos($url, $path), strlen($path));更新2
如果您的脚本被调用为script?link=<some-url>,则需要确保<some-url>也被正确编码:
script?link=https%3A%2F%2Fwww.example.com%2Fxxxxx%2Fname_more_names_%26_more_names_after_the_ampersand.file发布于 2016-09-05 12:11:37
使用
$url = urlencode($url);
$data = file_get_contents($url);https://stackoverflow.com/questions/15829266
复制相似问题