【问题标题】:PHP newbie CURL and arrayPHP 新手 CURL 和数组
【发布时间】:2010-07-08 10:51:09
【问题描述】:

更新:我已经简化了代码(尝试)

我正在尝试以数组的形式下载一系列图像,但显然有些不对:

function savePhoto($remoteImage,$fname) {
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_NOBODY, true);
    curl_setopt ($ch, CURLOPT_URL, $remoteImage);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
    $fileContents = curl_exec($ch);
    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if($retcode==200) {
        $newImg = imagecreatefromstring($fileContents);
        imagejpeg($newImg, ".{$fname}.jpg",100);
    }
    return $retcode;
}

$filesToGet = array('009');
$filesToPrint = array();

foreach ($filesToGet as $file) {
        if(savePhoto('http://pimpin.dk/jpeg/'.$file.'.jpg',$file)==200) {
            $size = getimagesize(".".$file.".jpg");
            echo $size[0] . " * " . $size[1] . "<br />";
        }
}

我收到以下错误:

警告:imagecreatefromstring() [function.imagecreatefromstring]: 中的空字符串或无效图像 C:\inetpub\vhosts\dehold.net\httpdocs\ripdw\index.php 在第 15 行

警告:imagejpeg():已提供 参数不是有效的图像资源 在 C:\inetpub\vhosts\dehold.net\httpdocs\ripdw\index.php 第 16 行

警告:getimagesize(.009.jpg) [function.getimagesize]:未能 打开流:没有这样的文件或目录 在 C:\inetpub\vhosts\dehold.net\httpdocs\ripdw\index.php 在第 26 行 *

【问题讨论】:

  • 大家好!我已经更新了问题:-)这是我在堆栈上的第一个 Q,所以我希望你能忍受我:)

标签: arrays curl php getimagesize


【解决方案1】:

试试这个:

function get_file1($file, $local_path, $newfilename)
{
    $err_msg = '';
    echo "<br>Attempting message download for $file<br>";
    $out = fopen($newfilename, 'wb');
    if ($out == FALSE){
      print "File not opened<br>";
      exit;
    }

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_FILE, $out);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL, $file);

    curl_exec($ch);
    echo "<br>Error is : ".curl_error ( $ch);

    curl_close($ch);
    //fclose($handle);

}//end function 

//取自:http://www.weberdev.com/get_example-4009.html

file_get_contents

【讨论】:

  • 我尝试了你的建议,但它没有将文件保存为 JPEG,我真的很想让我的代码工作(并希望了解我做错了什么):- ) 感谢您的意见
【解决方案2】:

您应该尝试使用 file_get_contents 来代替 CURL(更简单,但它可以完成工作):

 function savePhoto($remoteImage,$fname) {        
      $fileContents = file_get_contents($remoteImage);        
      try {        
        $newImg = imagecreatefromstring($fileContents);
        imagejpeg($newImg, ".{$fname}.jpg",100);        
      } catch (Exception $e) {        
        //what to do if the url is invalid        
      } 
}

【讨论】:

  • 这确实有助于保存文件,但如果文件不存在并返回 404,则会返回错误:... 警告:file_get_contents(pimpin.dk/jpeg/00x9.jpg) [function.file- get-contents]:打开流失败:HTTP 请求失败! HTTP/1.1 404 Not Found in C:\inetpub\vhosts\dehold.net\httpdocs\ripdw\index.php on line 22... 是否有可能阻止它?
  • 是的,把你的 $fileContents = file_get_contents($remoteImage);在尝试中。如果失败(在 404 的情况下),那么您的 catch 指令将被处理而不会引发警告(除非您在 catch 中抛出异常,显然)。
【解决方案3】:

在大家的帮助和一些窥探的帮助下,我终于让它工作了:-)

我最终使用了 CURL:

function savePhoto($remoteImage,$fname) {
    $ch = curl_init();

    curl_setopt ($ch, CURLOPT_URL, $remoteImage);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
    $fileContents = curl_exec($ch);

    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);
    if($retcode == 200) {
        $newImg = imagecreatefromstring($fileContents);
        imagejpeg($newImg, $fname.".jpg",100);
    }

    return $retcode;
}


$website = "http://www.pimpin.dk/jpeg";
$filesToGet = array('009');
$filesToPrint = array();

foreach ($filesToGet as $file) {
        if(savePhoto("$website/$file.jpg",$file)==200) {
            $size = getimagesize($file.".jpg");
            echo $size[0] . " * " . $size[1] . "<br />";
        } else {
            echo "File wasn't found";
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-14
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多