[英]Wordpress Medium API issue - 403 Forbidden on Azure
注意問題出現在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禁止
我認為您的問題在您的請求中缺少用戶代理。 最好將cURL與User-agent選項結合使用:
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>";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.