我有一个函数,它将一个 url 数组作为输入。我已经验证了 url 是正确的,我可以完美地循环它们。我还使用 curl_getinfo 验证了 curl 正在下载正确的页面。但是,每个页面的 curl (html) 输出都是相同的。这是我的代码:
$urls = array();
$urls = getpages($mainpage);
print_r($urls);
foreach($urls as $link) {
echo $link. '<br><br><br>';
$circdl = my_curl($link);
echo $circdl. '<br><br><br>';
$circdl = NULL;
}
url的输出数组如下:
Array ( [0] => http://www.site.com/savings/viewcircular?promotionId=81498&sneakpeek=¤tPageNumber=1 [1] => http://www.site.com/savings/viewcircular?promotionId=81498&sneakpeek=¤tPageNumber=2
$link 也像 curl_getinfo 中的 curl 一样正确输出。我通过这个循环运行了另一个 url 数组,它们工作正常,但我怀疑这里的问题是 url 的格式(与号)。我真的很难过为什么这些页面没有按预期下载。
这是 my_curl 函数:
function my_curl($url)
{
$timeout=10;
$error_report=TRUE;
$curl = curl_init();
$cookiepath = drupal_get_path('module','mymodule'). '/cookies.txt';
// HEADERS AND OPTIONS APPEAR TO BE A FIREFOX BROWSER REFERRED BY GOOGLE
$header[] = "Accept: text/xml,application/xml,application/xhtml+xml,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 USUALLY LEAVE BLANK
// SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
curl_setopt( $curl, CURLOPT_URL, $url );
curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6' );
curl_setopt( $curl, CURLOPT_HTTPHEADER, $header );
curl_setopt( $curl, CURLOPT_REFERER, 'http://www.google.com' );
curl_setopt( $curl, CURLOPT_ENCODING, 'gzip,deflate' );
curl_setopt( $curl, CURLOPT_AUTOREFERER, TRUE );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE );
curl_setopt( $curl, CURLOPT_COOKIEFILE, $cookiepath );
curl_setopt( $curl, CURLOPT_COOKIEJAR, $cookiepath );
curl_setopt( $curl, CURLOPT_TIMEOUT, $timeout );
// RUN THE CURL REQUEST AND GET THE RESULTS
$htm = curl_exec($curl);
// Check for page request
//$info = curl_getinfo($curl);
//echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
// ON FAILURE HANDLE ERROR MESSAGE
if ($htm === FALSE)
{
if ($error_report)
{
$err = curl_errno($curl);
$inf = curl_getinfo($curl);
echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
var_dump($inf);
}
curl_close($curl);
return FALSE;
}
// ON SUCCESS RETURN XML / HTML STRING
curl_close($curl);
return $htm;
}
非常有趣的是,如果我运行这个:
echo my_curl('http://www.site.com/savings/viewcircular?promotionId=81498&sneakpeek=¤tPageNumber=2')
输出是正确的!!??:(
谢谢你的帮助!