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回返す必要があることを意味しますが、これを実行すると、1つの結果のみが出力され、実行が停止します。

4

1 に答える 1

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



?>

CURLコードはこちらから

于 2012-11-27T09:40:22.390 に答える