簡體   English   中英

PHP / cURL錯誤7; 無法連接到主機

[英]PHP/cURL Error 7; Couldn't connect to host

因此,我一直遇到相同的錯誤...我已經花了幾個小時試圖找到解決方法,但似乎找不到丟失的部分。 許多其他人詢問堆棧溢出錯誤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);        

}

因此,為了使其正常工作,我不得不在該功能上加一個瓶頸,以使其稍慢一些。 正如安德魯西(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來創建瓶頸,則可以在downloadImage函數中使用php的sleep (秒)或usleep (微秒)函數來創建類似的瓶頸。

希望這個理論可以幫助別人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM