imgur api won't work

旧城冷巷雨未停 提交于 2019-12-19 10:26:29

问题


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


回答1:


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.




回答2:


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

Pulled my hair off for this



来源:https://stackoverflow.com/questions/4986470/imgur-api-wont-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!