douxi7219 2013-01-17 19:51
浏览 49
已采纳

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

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"
        }
    ]
}

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);
  • 写回答

1条回答 默认 最新

  • douyou2732 2013-01-17 20:05
    关注

    set

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c