【问题标题】:Using PHP to post an image to Cheezburger, json_decode won't handle the response使用 PHP 将图像发布到 Cheezburger,json_decode 不会处理响应
【发布时间】:2013-01-17 19:51:31
【问题描述】:

我正在尝试使用 PHP 脚本将图像发布到 Cheezburger.com,并将 URL 返回给用户。帖子部分工作正常,我以 JSON 格式获取链接、ID 等,但是当我运行 json_decode($var, true) 时,它只会返回原始 JSON。这是输入脚本的字符串:

{
    "items": [
        {
            "id": 6980805120,
            "link": "https://api.cheezburger.com/v1/assets/6980805120",
            "created_time": 1358451002,
            "updated_time": 1358451002,
            "media": [
                {
                    "name": "maxW580",
                    "url": "https://i.chzbgr.com/maxW580/6980805120/h89D91707/",
                    "height": 500,
                    "width": 500,
                    "is_animated": false
                },
                {
                    "name": "maxW320",
                    "url": "https://i.chzbgr.com/maxW320/6980805120/h89D91707/",
                    "height": 320,
                    "width": 320,
                    "is_animated": false
                },
                {
                    "name": "square50",
                    "url": "https://i.chzbgr.com/square50/6980805120/h89D91707/",
                    "height": 50,
                    "width": 50,
                    "is_animated": false
                }
            ],
            "title": "JSA, UR WEBSIET IZ AWSUM. URE HIRD!",
            "description": "JSA, UR WEBSIET IZ AWSUM. URE HIRD! -- This image was created by jsa005 from JSiVi using the JSiVi Meme Generator. Try it out at http://jsivi.uni.me!",
            "asset_type_id": 0,
            "share_url": "http://chzb.gr/10Cg1PS"
        }
    ]
}

当我在上面运行json_decode($jsonstring, true) 时,$jsonstring 是包含上述字符串的 cURL 返回的变量,我只取回我输入的字符串。我很困惑。

$fields = array(
'access_token' => $this->getToken(),
'title' => $title,
'description' => $description,
'content' => $base64data,
'anonymous' => 'true');
$url = 'https://api.cheezburger.com/v1/assets';
$fields_string = http_build_query($fields);
//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

$jsonstring = json_decode($result, TRUE);

【问题讨论】:

  • it only gives me the raw JSON back 是什么让你这么说?你是如何检查它的?
  • 使用 php 5.4 运行您通过 json_decode 提供的字符串返回了合理的结果。我只能建议您仔细检查 $result 的值是否与您作为示例提供的字符串完全匹配。
  • 是的。通过隔离 CURL 然后 json_decode 来不够缩小问题范围的标准案例。
  • 我在变量上使用了 print_r。
  • 感谢 Stack Overflow 社区的帮助,这是我正在使用的网站(您必须登录才能保存到 Cheezburger,因为它也会添加到我的数据库中):jsivi.uni.me/meme/gen_index.php

标签: php curl json


【解决方案1】:

设置

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

在运行curl_exec($ch); 之前,如果没有它,响应会直接打印到您的浏览器中,因此您会看到“原始”JSON 并且$response 是布尔值(TRUEFALSE)。 See manual page for more details

【讨论】:

  • 您想解释一下原因吗?
  • "有时您需要从 curl_exec 获取响应并捕获传输 - 这没有很好的文档记录,但您可以使用 CURLOPT_RETURNTRANSFER 选项来实现" from the documentation.
  • 谢谢,问题已经解决。 ;)
猜你喜欢
  • 2010-10-03
  • 2018-11-12
  • 2015-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-26
相关资源
最近更新 更多