[英]PHP CURL returns null
我正在嘗試從遠程站點獲取頁面內容。 它適用於許多站點。 但是某些網址,例如http://www1.macys.com/,則不會返回任何內容。 誰能告訴我解決方案或問題是什么? 我想念什么嗎?
如果我使用的是fopen()或file_get_contents(),則會顯示警告“已達到重定向限制,正在中止”
下面是我的代碼。
<?php
$url = 'http://www1.macys.com/shop/product/volcom-stripe-thermal-shirt?ID=1155481&CategoryID=30423#fn=sp%3D1%26spc%3D996%26ruleId%3D27%26slotId%3D1';
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$contents = curl_exec($ch);
if(curl_errno($ch)) {
echo 'Error: ' . curl_error($ch) . '<br><br>';
}
echo 'Contents: '; print_r($contents); echo '<br><br>';
curl_close($ch);
?>
也許是重定向問題。嘗試添加以下內容:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
這個選項讓cUrl跟隨重定向
編輯:
還要添加以下內容:
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).DIRECTORY_SEPERATOR.'cookie.txt');
記住將cookie.txt的權限設置為777
除非您維護一個餅干罐,否則某些網站不會提供圖像。
試試這個:(來自: https : //stackoverflow.com/a/12885587/2167896 )
$jar = tmpfile();
$output = fetch('www.google.com', $jar)
function fetch( $url, $z=null ) {
$ch = curl_init();
$useragent = isset($z['useragent']) ? $z['useragent'] : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2';
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_POST, isset($z['post']) );
if( isset($z['post']) ) curl_setopt( $ch, CURLOPT_POSTFIELDS, $z['post'] );
if( isset($z['refer']) ) curl_setopt( $ch, CURLOPT_REFERER, $z['refer'] );
curl_setopt( $ch, CURLOPT_USERAGENT, $useragent );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, ( isset($z['timeout']) ? $z['timeout'] : 5 ) );
curl_setopt( $ch, CURLOPT_COOKIEJAR, $z['cookiefile'] );
curl_setopt( $ch, CURLOPT_COOKIEFILE, $z['cookiefile'] );
$result = curl_exec( $ch );
curl_close( $ch );
return $result;
}
如果代碼與其他URL一起使用,則可能是特定服務器阻止了curl請求。 嘗試fopen()
。
或添加適當的標題和引用,這是我使用的:
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: "; //browsers keep this blank.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com');
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$contents = curl_exec($ch);
嘗試添加“ USERAGENT”,即您的api用戶名,網站名稱或其他名稱:
curl_setopt($ch, CURLOPT_USERAGENT, 'MY-NAME');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.