使用PHP将图像发布到Cheezburger,json#u decode赢得';我无法处理这个问题
我试图用一个PHP脚本将一个图像发布到Cheezburger.com,并将URL返回给用户。post部分工作正常,我获得了JSON格式的链接、ID等,但当我运行使用PHP将图像发布到Cheezburger,json#u decode赢得';我无法处理这个问题,php,curl,json,Php,Curl,Json,我试图用一个PHP脚本将一个图像发布到Cheezburger.com,并将URL返回给用户。post部分工作正常,我获得了JSON格式的链接、ID等,但当我运行JSON\u decode($var,true)时,它只返回原始JSON。以下是输入脚本的字符串: { "items": [ { "id": 6980805120, "link": "https://api.cheezburger.com/v1/assets/6980
JSON\u 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\u 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);
设置
在运行
curl\u exec($ch)之前
如果没有它,响应将直接打印到浏览器中,因此您将看到“原始”JSON,$response
是布尔值(TRUE
或FALSE
) 它只会把原始的JSON还给我
你为什么这么说?如何检查它?使用PHP5.4运行通过json_解码给出的字符串返回了一个合理的结果。我所能推荐的是,您需要再次检查$result的值是否与您作为示例提供的字符串完全匹配。是的。通过隔离CURL然后json\u decode
来缩小问题范围的标准案例。我在变量上使用了print\r。作为对有用的Stack Overflow社区的感谢,这里是我使用它的网站(你必须登录才能保存到Cheezburger,因为它也会添加到我的DB中):你想解释为什么吗?“有时您需要从curl_exec获取响应并捕获传输-这没有很好的文档记录,但您可以使用CURLOPT_RETURNTRANSFER选项来完成。谢谢,这解决了问题。;)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);