使用PHP将图像发布到Cheezburger,json_decode将不会处理响应 [英] Using PHP to post an image to Cheezburger, json_decode won't handle the response
问题描述
我试图使用PHP脚本将图像发布到Cheezburger.com,并将URL返回给用户。 post部分工作正常,我得到的链接,ID等以JSON格式,但是当我运行 json_decode($ var,true)
它只给我原始的JSON背部。下面是送入脚本的字符串:
I am trying to post an image to Cheezburger.com with a PHP script, and return the URL to the user. The post part works fine, I get the links, IDs etc. back in JSON format, but when I run json_decode($var, true)
it only gives me the raw JSON back. Here is the string that got fed into the script:
{
"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返回的包含上面的字符串的变量,我只得到回馈的字符串。我很困惑。
When I run json_decode($jsonstring, true)
on that, $jsonstring
being the variable returned by cURL containing the string above, I only get back the string I fed in. I am confused.
$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);
推荐答案
设定
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
<没有它,响应直接打印到您的浏览器,所以你看到原始JSON和 $响应
是布尔值( TRUE
或 FALSE
)。 有关详情,请参阅手册页
before running curl_exec($ch);
Without it, response is printed directly into your browser, so you are seeing "raw" JSON and $response
is boolean value (TRUE
or FALSE
). See manual page for more details
这篇关于使用PHP将图像发布到Cheezburger,json_decode将不会处理响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!