【问题标题】:how to stop & from changing to &amp in a url?如何阻止 & 从网址中的 &amp 更改?
【发布时间】:2017-01-15 17:35:50
【问题描述】:

我有一个这样的网址,https://www.example.com/send?id=1&text=http://example2.com/song.mp3

当我尝试使用 file_get_contents() 获取 url 的数据时,它显示以下错误,

PHP 警告:file_get_contents(https://www.example.com/send?id=1&amptext=http://example2.com/song.mp3): 无法打开流:HTTP 请求失败! HTTP/1.1 501 未实现

并且 & 已转换为 &amp。由于该网址无效。

我在 & 上尝试过 htmlspecialchars_decode()preg_replace(),但没有任何效果。

如何解决这个问题?

【问题讨论】:

  • @Xorifelse no 用 %26 转义并没有解决问题
  • 引用网址是http还是https?
  • 尝试使用 curl,我在下面发布并希望它能工作
  • 现在工作了吗?

标签: php url htmlspecialchars


【解决方案1】:

我认为您最好使用cURL 而不是file_get_contents(),因为如果您使用file_get_contents(),则必须在他们的服务器中启用引用服务器allow_url_fopen,但cURL 是一个库,它只是发出一个http 请求

<?php
    $cSession = curl_init(); 
    curl_setopt($cSession,CURLOPT_URL,"https://www.example.com/send?id=1&text=http://example2.com/song.mp3");
    curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($cSession,CURLOPT_HEADER, false); 
    $result=curl_exec($cSession);
    curl_close($cSession);
    echo $result;
?> 

参考以下链接

file_get_contents script works with some websites but not others

cURL vs file_get_contents

【讨论】:

  • url 中的 '&' 前后似乎有一个空格。您可以尝试删除空格
【解决方案2】:

看看这位先生,$str="your link"

样本

$str = "https://www.example.com/send?id=1&text=http://example2.com/song.mp3";

echo html_entity_decode($str);

会输出你想要的....

【讨论】:

    猜你喜欢
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    • 2016-09-26
    • 1970-01-01
    相关资源
    最近更新 更多