使用PHP将图像发布到Cheezburger,json_decode将无法处理响应

时间:2013-01-17 19:51:31

标签: php curl json

我正在尝试使用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);

1 个答案:

答案 0 :(得分:2)

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

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