0

私はGDライブラリを使用して画像を作成し、APIを使用してImageShackに直接アップロードするスクリプトを考え出そうとしています。私は Elliott C. Back によって書かれた ImageShack クラスを使用しています。このクラスは、ファイルから画像をアップロードすることで、変更なしで問題なく動作します。しかし、CURL Options コードの単純な 1 行を変更しようとすると、コードが壊れてアップロードされません。

単純に黒い画像を作成して変数に保存するサンプルコードを作成しました(ローカルファイルに保存するだけでなく、変数などに保存して破損しないかどうかを確認します)。 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;
    }
}
?>
4

1 に答える 1

0

いくつかの洞察:

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

コンテンツが NULL であることを意味します。$_POST['somevar'];これは通常、somevar が存在しない場合など、Array を使用する場合に発生します。スクリプトに必要なデータを提供していますか。

変数にエラー メッセージの各行番号のデータが実際に含まれていることを確認してください。

于 2010-09-15T04:23:46.523 に答える