【问题标题】:imgur api won't workimgur api 不起作用
【发布时间】:2011-02-13 19:52:17
【问题描述】:

我对@9​​87654321@ api 有疑问。 我想使用 imgur api 为我的网站创建一个图库,但是如何创建一个上传到 imgur 服务器的文件上传器?

这是我创建的:

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

但这似乎不起作用......? 我收到此错误:

解析错误:语法错误,意外的 T_STRING,在第 7 行的 C:\data\home\www\test.php 中需要 ')'

第 7 行是这一行:

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

怎么了? imgur api 的文档在这里: http://api.imgur.com/examples

你们能帮帮我吗?

是的,我已经搜索过这些主题:

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)

但这对我没有帮助......

问候

【问题讨论】:

  • 您是否将 API 密钥放在引号中?
  • @Daniel:不,文档说我不能那样做......
  • 文档中的什么地方这么说的?
  • @Daniel,好吧,我查看了示例,所以在 php 示例第 7 行。
  • 它返回:“无效的 api 密钥”,它是一个有效的,在我的个人资料中检查。

标签: php api imgur


【解决方案1】:

将 API 密钥放在引号中。他们把它放在所有大写和外部引号中的方式是表示一个常量值。

【讨论】:

  • 这行不通!它说api不正确!
  • 这解决了您的第一个问题。第二个问题是您的 API 密钥,而不是您的 PHP 代码。
  • 嗯,是的。我会联系 imgur 关于这个。不过谢谢。
【解决方案2】:

确保您在此处注册匿名 API 而不是经过身份验证的 API http://imgur.com/register/api

为此拉了我的头发

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 2015-11-01
    • 2015-05-02
    • 1970-01-01
    相关资源
    最近更新 更多