PHP Curl类不会直接从变量上传文件,哪里是catch?

时间:2010-09-15 01:17:43

标签: php gd imageshack

我一直在努力想出一个使用GD库创建图像的脚本,并使用他们的API将其直接上传到ImageShack。我正在使用Elliott C. Back编写的ImageShack类,它通过从文件上传图像而完美无缺地工作。但是当我尝试修改一行简单的CURL选项代码时,它会中断并且不会上传。

我编写了一个示例代码,它只是简单地创建了黑色图像,将其存储到变量中(以及尝试将其保存在本地文件中 - 以验证它是否会因将其存储在变量或其他内容而损坏),以及最后尝试上传到ImageShack。代码被评论,所以它应该很容易阅读。我希望你能帮助我解决这个问题,因为我已经尝试过所有我能想到的东西。甚至可以使用CURL从变量上传文件吗?

当我运行这个脚本时,我遇到了一堆错误,但都是:

PHP Notice: Undefined offset: 1 in test.php on line 82

PHP Notice: Undefined offset: 2 in test.php on line 82

#!/usr/bin/php -q

<?php
// Create some basic image using PHP manual example for GD library
$img = imagecreatetruecolor(200, 200);

// Create new object from class ImageShack
$imageshack = new ImageShack;

// Store image to variable $image
ob_start();
ImagePNG($img);
$image = ob_get_clean();

// Upload image to ImageShack and print the url
$imageshack->upload($image);
$url = $imageshack->get_image_url();
print $url;

// And now let's try to save image from $image variable to local file to see if it's not corruped or anything
$tmpFile = "tmpFile.png";
$fh = fopen($tmpFile, 'w') or die("Can't open file");
fwrite($fh, $image);
fclose($fh);

// And finally destroy the image to free memory.
imagedestroy($img);

// Follows the slightly modified version of ImageShack class by Elliott C. Back
// only modified part is CURLOPT_POSTFIELDS part of upload() function and removed not required functions
// Class works flawlessly if you use it unmodified and upload from file, instead of variable.
class ImageShack
{
    var $is_url = "http://www.imageshack.us/index.php";
    var $is_result = false;
    var $is_result_parsed = false;

    public function upload( $file )
    {
        // send the image to ImageShack
        $ch = curl_init($this->is_url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 240);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'xml'=>'yes', 'fileupload'=>$file ));
  // curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'xml'=>'yes', 'fileupload'=>'@'.$file )); <== This is original line
        curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect: ' ));
        $this->is_result = curl_exec($ch);
        curl_close($ch);

        // Parse the result
        $this->parse();
    }

    public function get_image_url()
    {
        return $this->get( "image_link" );
    }

    private function get( $key )
    {
        if( !$this->is_result_parsed )
            return false;

        return( $this->is_result_parsed[ $key ] );
    }

    private function parse()
    {
        if (strpos($this->is_result, '<'.'?xml version=”1.0? encoding="iso-8859-1??>') === false)
            $this->is_result_parsed = false;

        $xmlData = explode("\n",$this->is_result);
        $xmlr = array();

        foreach($xmlData as $xmlDatum){
            $xmlDatum = trim($xmlDatum);

            if($xmlDatum != "" && !eregi("links",$xmlDatum) && !eregi("xml",$xmlDatum)){
                $xmlDatum = str_replace(">","<",$xmlDatum);
                list($xmlNull,$xmlName,$xmlValue) = explode("<",$xmlDatum);
                $xmlr[$xmlName] = $xmlValue;
            }
        }

        $this->is_result_parsed = $xmlr;
    }
}
?>

1 个答案:

答案 0 :(得分:0)

一些见解:

PHP Notice: Undefined offset: 1 in test.php on line 82 

表示内容为NULL。当somevar不存在时,通常在使用$_POST['somevar'];等数组时会发生这种情况。您是否在脚本中提供了所需的数据。

检查您的变量是否实际包含错误消息中每个行号的数据。