Php 多次卷曲
上面的代码是通过php中的cron作业运行的。在hotelsPhp 多次卷曲,php,curl,cron,Php,Curl,Cron,上面的代码是通过php中的cron作业运行的。在hotels表中有5个trip\u url。这意味着curl应该执行5次,并从服务器返回结果5次,但是 但当我运行此操作时,只会打印一个结果并停止执行。尝试一下: $hotel_query = "select hotel_id,hotel_name,trip_url,automatic_status from hotels where automatic_status='0'"; $hotel_result = mysql_query($hotel
表中有5个trip\u url。这意味着curl应该执行5次,并从服务器返回结果5次,但是
但当我运行此操作时,只会打印一个结果并停止执行。尝试一下:
$hotel_query = "select hotel_id,hotel_name,trip_url,automatic_status from hotels where automatic_status='0'";
$hotel_result = mysql_query($hotel_query) or die(mysql_error());
while($hotel_row = mysql_fetch_object($hotel_result))
{
$url=$hotel_row->trip_url;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo curl_error($ch);
echo $result;
}
尝试使用以下方法:mysql\u fetch\u array($hotel\u result))是否确定查询正常。。。试着把打印出来($hotel\u row);在while循环中,这样它每次都会回响。如果这给了你5个结果,那么我们可以继续前进。是的,它给了5个结果。@christ当我认为你的Curl代码可能有缺陷时,试试我在下面发布的代码-看看会发生什么Curl\u error($ch)代码>在curl\u关闭($ch)之后代码>。此时,$ch
不再是一个curl资源。您是否尝试手动打开链接以确保页面存在?好的-那么它是五次给出相同的1结果,还是只输出一次就消失了尝试上面更新的新代码
<?php
function curl_download($Url){
// is cURL installed yet?
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
// OK cool - then let's create a new cURL resource handle
$ch = curl_init();
// Now set some options (most are optional)
// Set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);
// Set a referer
curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Download the given URL, and return output
$output = curl_exec($ch);
// Close the cURL resource, and free system resources
curl_close($ch);
return $output;
}
$loop_counter = 1;
$hotel_query = "select hotel_id,hotel_name,trip_url,automatic_status from hotels where automatic_status='0'";
$hotel_result = mysql_query($hotel_query) or die(mysql_error());
while($hotel_row = mysql_fetch_object($hotel_result)){
$url=$hotel_row->trip_url;
echo "Loop no.".$loop_counter."<br />";
echo curl_download($url);
$loop_counter++;
}
?>