【问题标题】:Wordpress Medium API issue - 403 Forbidden on AzureWordpress Medium API 问题 - Azure 上的 403 Forbidden
【发布时间】:2018-01-17 02:21:10
【问题描述】:

注意问题出现在 Azure 服务器中。它不允许获取 CURL 的详细信息。

我在页脚中显示帖子有问题。当我尝试通过file_get_content 获取它以及使用CURL 但仍然有问题。

$url="https://medium.com/@username/latest?format=json";
$homepage = file_get_contents($url);

echo "<pre>";
print_r($homepage);
echo "</pre>";

它在 Linux 中工作,但是当我在此之后移动到 Windows Azure 时会出现以下错误。

警告: file_get_contents(https://medium.com/@username/latest?format=json) [function.file-get-contents]:无法打开流:HTTP 请求 失败的! HTTP/1.1 403 禁止

【问题讨论】:

标签: php wordpress azure


【解决方案1】:

我认为您的问题是您的请求中缺少用户代理。更好地使用带有用户代理选项的 cURL:

function curl_get_contents($url){
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
   $data = curl_exec($ch);
   curl_close($ch);
   return $data;
}

现在您可以像这样获取端点的内容:

$url="https://medium.com/@hackernoon/latest?format=json";
$homepage = curl_get_contents($url);

echo "<pre>";
print_r($homepage);
echo "</pre>";

【讨论】:

  • 是的,它说同样的话。让我现在向你解释一下我在 Azure 中的 wordpress。
  • 请您尝试获取一些其他网站内容,例如httpbin.org/ip。如果成功,可能是 Medium 阻止了您的 IP 请求。
  • 它适用于另一个托管在 linux 服务器上的网站。
  • Medium中有允许域的设置吗?
  • 嗯。我对 Azure 不熟悉,因此无法为您提供建议。但这只有两个问题:1.我猜是媒体阻止了你的IP,2.你的服务器证书有问题。所以在curl_exec 之前添加这行curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 然后再试一次。希望对您有所帮助。
猜你喜欢
  • 2017-02-14
  • 1970-01-01
  • 1970-01-01
  • 2012-02-17
  • 2018-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多