2012-06-26 66 views
1

因此,我一直遇到同樣的錯誤...我已經搜索了幾個小時試圖找到一個決議,但我似乎無法找到缺失的一塊。很多其他人在堆棧溢出時詢問錯誤7,但沒有一個與我的方案相似。PHP/cURL錯誤7;無法連接到主機

基本上,我使用cURL來下載通過XML feed發送的圖像。我的整個腳本都能正常工作,一切都在運行,我下面寫的功能甚至可以下載數以千計的圖像(有時會上傳到3000範圍)。

我想我的問題是,爲什麼在下載3000張圖像後它不能連接?

function downloadImage($location, $imagesPath, $imageName) { 

    //Location fix 
    $location = str_replace(" ", "%20", $location); 

    $url = $location; 
    $path = $imagesPath . $imageName; 
    $fp = fopen($path, 'w');  

    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_FILE, $fp); 
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); //Wait indefinitately  
    curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false); 

    $data = curl_exec($ch); 
    if ($data === false) { 
     echo "DownloadImage cURL failed 1: (" . curl_errno($ch) . ") " . curl_error($ch) . "<br/>"; 
     //exit; 
    }   
    curl_close($ch); 


    fclose($fp);   

} 
+8

遠程站點是否會因爲太快下載太多圖片而中斷您的操作? – andrewsi

+1

如果服務器支持它,你應該發送一個保持活動的頭,並保持重複使用相同的捲曲手柄進行後續下載。它應該提高性能並可能解決您的問題。 – drew010

+0

$位置是否改變?如果不是,你可以打開連接,然後關閉所有3000+圖像完成後關閉? – craig1231

回答

1

所以爲了使這個工作,我不得不把一個瓶頸緩慢下來一點。正如Andrewsi所建議的那樣,這個遠程站點因爲下載鏡像太快而被迫關閉。爲了阻止這個功能,我將每個鏡像都FTP到遠程服務器上(因爲無論如何這都是必需的)。

最終功能是這樣的:

function downloadImage($location, $imagesPath, $imageName, $ch3, $feedFTPinfo) { 

//Location fix 
//$location = str_replace(" ", "%20", $location); 

$url = $location; 
$path = $imagesPath . $imageName; 

$fp = fopen($path, 'w');  
$ch2 = curl_init(); // Initiate cURL for downloading images 

//Setup the cURL options for the second handle ($ch2) 
curl_setopt($ch2, CURLOPT_URL, $url); 
curl_setopt($ch2, CURLOPT_FILE, $fp); 
curl_setopt($ch2, CURLOPT_CONNECTTIMEOUT, 0); //Wait indefinitately  
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true); 

//Execute the cURL session 
$data2 = curl_exec($ch2); 

//Resize Images 
$image = new SimpleImage(); 
$image->load($url); 
$imageNameSm = str_replace(".jpg", "", $imageName); 
$imageNameSm = $imageNameSm."_sm2.jpg"; 
$image->resizeToWidth(120); 
$image->save($imagesPath . $imageNameSm); 
$smPath = $imagesPath . $imageNameSm; 

//Find out if there were any issues 
if ($data2 === false) { 
    echo "DownloadImage cURL failed 1: (" . curl_errno($ch2) . ") " . curl_error($ch2) . "<br/>"; 
    //exit; 
} else { 
    //There weren't any issues downloading the file, move it to the speficifed ftp server 

    if (!empty($feedFTPinfo)) { 

     $localfile = $path; 
     $fp = fopen($localfile, 'r'); 

     //Setup the options for the 3rd handle 
     curl_setopt($ch3, CURLOPT_URL, $feedFTPinfo.$imageName); 
     curl_setopt($ch3, CURLOPT_CONNECTTIMEOUT, 0); //Wait indefinitately 
     curl_setopt($ch3, CURLOPT_UPLOAD, 1); 
     curl_setopt($ch3, CURLOPT_INFILE, $fp); 
     curl_setopt($ch3, CURLOPT_INFILESIZE, filesize($localfile)); 

     //Execute the 3rd cURL handle 
     $data3 = curl_exec($ch3); 

     //Find out if there were any issues with the execution 
     if ($data3 === false) { 
      echo "Uploading the image via FTP failed: (" . curl_errno($ch3) . ") " . curl_error($ch3) . "<br/>"; 
      //exit; 
     } 

     $localfile = $smPath; 
     $fp = fopen($localfile, 'r'); 

     //Setup the options for the 3rd handle 
     curl_setopt($ch3, CURLOPT_URL, $feedFTPinfo.$imageNameSm); 
     curl_setopt($ch3, CURLOPT_CONNECTTIMEOUT, 0); //Wait indefinitately 
     curl_setopt($ch3, CURLOPT_UPLOAD, 1); 
     curl_setopt($ch3, CURLOPT_INFILE, $fp); 
     curl_setopt($ch3, CURLOPT_INFILESIZE, filesize($localfile)); 

     //Execute the 3rd cURL handle 
     $data3 = curl_exec($ch3); 

     //Find out if there were any issues with the execution 
     if ($data3 === false) { 
      echo "Uploading the small image via FTP failed: (" . curl_errno($ch3) . ") " . curl_error($ch3) . "<br/>"; 
      //exit; 
     }   




    } 


} 

curl_close($ch2); //Close the cURL handle that downloads images 
fclose($fp); 
} 

如果你不需要某個FTP到創建的瓶頸,你可以使用PHP的sleep(秒)或usleep(微秒)功能,您downloadImage功能內造成類似的瓶頸。

希望這個理論有助於別人了。

相關問題