PHP Curl 类不会直接从变量上传文件,问题在哪里?

发布于 2024-09-19 12:32:49 字数 3401 浏览 6 评论 0原文

我一直在尝试使用 GD 库编写一个脚本来创建图像并使用他们的 API 将其直接上传到 ImageShack。我正在使用 Elliott C. Back 编写的 ImageShack 类,该类通过从文件上传图像而无需修改即可完美运行。但是,当我尝试修改一行简单的 CURL Options 代码时,它会中断并且无法上传。

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

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

PHP 注意:未定义的偏移:第 82 行 test.php 中的 1

PHP 注意:测试中未定义的偏移:2 .php 第 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;
    }
}
?>

I've been trying to come up with a script using GD library to create an image and upload it straight to ImageShack using their API. I am using ImageShack class written by Elliott C. Back, which works flawlessly unmodified by uploading image from file. But when I try to modify one simple line of CURL Options code, it breaks and won't upload.

I have written a sample code that simply creates black image, store it into variable (as well as try to save it in local file - to verify if it won't become corrupted by storing it in variable or anything), and finally tries to upload to ImageShack. Code is commented so it should be rather easy to read. I hope you will be able to help me with this one, as I have already tried everything I could think of. Is it even possible to upload file using CURL from variable?

When I run this script I get bunch of errors, but all are either:

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

or

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;
    }
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺深海 2024-09-26 12:32:49

一些见解:

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

意味着内容为NULL。当 somevar 不存在时使用诸如 $_POST['somevar']; 之类的数组时,通常会发生这种情况。您是否为脚本提供了所需的数据?

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

Some insight:

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

means that the content is NULL. This usually occurs when using an Array such as $_POST['somevar']; when somevar does not exist. Are you providing the script with the data it needs.

Check that your variables actually contain data for each line number in the error message.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文