imgur api won't work

2019-02-19 18:26发布

i have a question about the imgur api. I want to create a gallery for my website using the imgur api, but how can i create a file uploader that uploads to the imgur servers?

Here is what i created:

<?php
include 'xmlparser.php'; // From http://www.criticaldevelopment.net/xml/doc.php
if($_SERVER['REQUEST_METHOD'] == "POST"){
    $data = file_get_contents($_FILES["file"]['tmp_name']);

    // $data is file data
    $pvars   = array('image' => base64_encode($data), 'key' => HERE_MY_API_KEY);
    $timeout = 30;
    $curl    = curl_init();

    curl_setopt($curl, CURLOPT_URL, 'http://api.imgur.com/2/upload.xml');
    curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);

    $xml = curl_exec($curl);

    $parser = new XMLParser($xml);
    $parser->Parse();
    echo $parser->images->item->links->original;

    curl_close ($curl); 
}
else
{
    ?>
    <form action="test.php" method="post" enctype="multipart/form-data">
        <input type="file" name="file" id="file" /> 
        <input type="submit" name="submit" value="Submit" />
    </form>
    <?php
} 
?>

But this doesn't seem to work...? I get this error:

Parse error: syntax error, unexpected T_STRING, expecting ')' in C:\data\home\www\test.php on line 7

And line 7 is this row:

$pvars   = array('image' => base64_encode($data), 'key' => HERE_MY_API_KEY);

What is wrong? The documentation of the imgur api is here: http://api.imgur.com/examples

Can you guys help me?

And yes, i already searched through these topics:

HTML Upload Form will only upload files found in the directory of the PHP file
Using jQuery to parse XML returned from PHP script (imgur.com API)

But it didn't help me...

Greetings

标签: php api imgur
2条回答
乱世女痞
2楼-- · 2019-02-19 18:34

Put the API key in quote marks. The way they put it in all caps and outside quote marks is to signify a constant value.

查看更多
倾城 Initia
3楼-- · 2019-02-19 18:46

Make sure you register for anonymous API and not Authenticated API here http://imgur.com/register/api

Pulled my hair off for this

查看更多
登录 后发表回答