简体   繁体   English

PHP Curl类不会直接从变量上传文件,这有什么用呢?

[英]PHP Curl class won't upload file straight from variable, where's the catch?

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. 我一直在尝试使用GD库创建脚本来创建图像,然后使用其API直接将其上传到ImageShack。 I am using ImageShack class written by Elliott C. Back, which works flawlessly unmodified by uploading image from file. 我正在使用Elliott C. Back编写的ImageShack类,该类可以通过从文件上传图像而完美地进行修改。 But when I try to modify one simple line of CURL Options code, it breaks and won't upload. 但是,当我尝试修改一行简单的CURL选项代码时,它会中断并且不会上传。

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. 我编写了一个示例代码,该代码简单地创建了黑色图像,将其存储到变量中(以及尝试将其保存在本地文件中,以通过将其存储在变量中或其他任何内容来验证它是否不会损坏),最后尝试上传到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? 甚至可以使用CURL从变量上传文件吗?

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

Some insight: 一些见解:

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

means that the content is NULL. 表示内容为NULL。 This usually occurs when using an Array such as $_POST['somevar']; 这通常在使用$_POST['somevar'];类的数组时发生$_POST['somevar']; when somevar does not exist. 当somevar不存在时。 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. 检查错误消息中的变量是否实际包含每个行号的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM