0

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

1 回答 1

2

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

在没有它运行之前curl_exec($ch);,响应会直接打印到您的浏览器中,因此您会看到“原始” JSON 并且$response是布尔值(TRUEFALSE)。有关详细信息,请参阅手册页

于 2013-01-17T20:05:36.617 回答