【问题标题】:Get returned message using CURL-GET submission使用 CURL-GET 提交获取返回的消息
【发布时间】:2013-05-29 04:21:45
【问题描述】:

我想使用 PHP 将返回的 JSON 消息从下面的 url 存储到某种变量中。

在我的控制器里面我有这个代码:

$ch=curl_init();
curl_setopt($ch, CURLOPT_URL,'http://www.livepicly.com/app/api.php'.$str);
curl_exec($ch);
curl_close($ch);

($str 是一个包含所有必需参数的变量)。

上述网址:

http://www.livepicly.com/app/api.php?method=add_reservation&email=jbond%40example.com&fname=James&lname=Bond&phone=1234561&vid=726&size=2&date=2013-05-31+1%3A15+PM&request=Hi+there

(上面的url打开时会返回一个JSON消息)

【问题讨论】:

    标签: php json curl get


    【解决方案1】:
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL,'http://www.livepicly.com/app/api.php'.$str);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec($ch);
    curl_close($ch);
    
    $my_array = json_decode($data,true);  // <-- turns the returned json into an assoc array
    $my_obj =  json_decode($data); // <-- or into an object
    
    // {"foo-bar": 12345}
    echo $my_array['foo-bar'];   // outputs 12345
    echo $my_obj->{'foo-bar'};  // outputs 12345
    

    【讨论】:

    • 感谢提醒这部分 $my_array = json_decode($data,true);我一直在尝试没有真正的参数。
    • 解码中的清晰响应,注意到您最初忘记了“CURLOPT_RETURNTRANSFER”:P
    【解决方案2】:

    您需要退回转账。

    <?php
    
    $str = '?method=add_reservation&email=jbond%40example.com&fname=James&lname=Bond&phone=1234561&vid=726&size=2&date=2013-05-31+1%3A15+PM&request=Hi+there';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,'http://www.livepicly.com/app/api.php'.$str);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $result = curl_exec($ch);
    curl_close($ch);
    $result = json_decode($result);
    var_dump($result->result[0]->message);
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-16
      • 1970-01-01
      • 2013-05-03
      • 2011-05-22
      • 2017-01-18
      • 1970-01-01
      相关资源
      最近更新 更多