1

そのため、同じエラーが発生し続けます...解決策を見つけるために何時間も検索しましたが、欠落している部分が見つからないようです. スタック オーバーフローのエラー 7 について多くの人が質問していますが、私のシナリオに似たものはありませんでした。

基本的に、XML フィードを介して送信される画像をダウンロードするために cURL を使用しています。私のスクリプト全体が機能し、すべてが実行され、以下に記述した関数は何千もの画像をダウンロードします (時には 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);        

}
4

1 に答える 1

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を使用して、同様のボトルネックを作成できます。

この理論が他の誰かを助けることを願っています。

于 2012-07-31T17:15:35.817 に答える