2012-11-27 21 views
0
$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; 
} 

上述代碼通過php中的cron作業運行。酒店中有5個trip_url酒店table。這意味着curl應該執行5次並從服務器返回結果5次,但是返回 但是當我運行這個時,只有一個結果被打印並停止執行。CURL多次

+0

嘗試使用這樣的:mysql_fetch_array($ hotel_result)) – Chris

+0

不,它不工作太 –

+0

你確保查詢工作...嘗試把print_r($ hotel_row);在while循環內部,這樣每次都會迴響。如果這給了你5個結果,那麼我們可以從那裏移動 – Chris

回答

0

試試這個:

<?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++; 
} 



?> 

捲曲代碼爲here

+0

沒有親愛的它不再工作 –

+0

您是否嘗試過手動打開鏈接以確保網頁存在? – Chris

+0

是的頁面存在... –