【发布时间】: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