php - 当 url 有 & 时,file_get_contents 失败

标签 php html url file-get-contents htmlspecialchars

我在尝试使用时遇到问题

    file_get_contents('');

请求一个url的内容但是php不断添加

    amp;

对于我尝试请求的具有 & 符号的 url 的每个部分,就像下面的一样

    http:site.com/api/user.php?id=1&gender=male&tag=coolman

现在,如果我自己在浏览器中加载这个 url,它会完美运行,我可以看到内容,但是当我尝试使用上面的函数在 php 中获取内容时,它会尝试像下面那样加载 url

    http:site.com/api/user.php?id=&gender=&tag=

并且因为它添加了 amp;请求每次都失败有人知道这是为什么以及我该如何阻止它吗?

最佳答案

在这种情况下,您可以使用 htmlspecialchars_decode()

<?php

$url = "http://example.com/api/user.php?id=&amp;gender=&amp;tag=coolman";
$url = htmlspecialchars_decode($url);

$content = file_get_contents($url);

?>

阅读更多:

http://php.net/manual/en/function.htmlspecialchars-decode.php

关于php - 当 url 有 & 时,file_get_contents 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29069175/

相关文章:

php - 使用 usort() 按层次排序标准对二维数组进行排序

php - 如何通过jquery post使用php mysql上传文件?

html - 为什么我的表单上的按钮忽略了字体大小属性?

javascript - jquery ajax字符限制

HTTP 413 请求实体太大

php - 使用前验证 SQL 文件

javascript - bxSlider javascript 转义 ' 语法

html - 垂直对齐适用于 block 元素

JAVA:从 HTTPS URL 获取响应代码的最快、最好的方法是什么?

javascript - 检查 JavaScript 字符串是否为 URL