doujieluo5875 2015-02-03 13:58
浏览 208

通过cURL POST PHP传递JSON

I've been trying to pass JSON though my web application using cURL - and I am a little stuck now.

Here is what I've tried :

Step 1:

I tried to post JSON using this

<?php 

  public function post(){

    $cars = array("Volvo", "BMW", "Toyota");
    $json = json_encode($cars);

    $ch = curl_init("http://localhost/api_v2/url?key=***");

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('json' => $json));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    curl_close($ch);

  }

?>

Step 2:

I tried to receive JSON back using this

public function get(){

        $json = json_decode(Input::get('json'));
        dd($json); // null

    }

Result: - I keep getting null when I do dd($json);

Can someone please help me point out what did I do wrong ?


Detail :

  • I use PHP Framework : Laravel 4.0
  • I am positive that URL parameter is correct because I can go to it
  • I am also sure that the JSON is not broken because after added print("<h1> JSON </h1><pre>".print_r($json,true)."</pre><br><hr><br>"); I can see my JSON display fine.

  • See Image

  • 写回答

3条回答

  • dongtuota3633 2015-02-03 15:43
    关注

    As far as your debugging efforts, you should dump the response rather than json_decode(s) attempt to parse it.

    So change this

    public function get() {
        $json = json_decode(Input::get('json'));
        dd($json); // null
    }
    

    to this

    public function get() {
        dd(Input::get('json'));
    }
    

    That should better help you track down the real problem, which most likely is that the server isn't responding with valid JSON.

    Another option is to use json_last_error to see why the response was unparseable.

    public function get() {
        $json = json_decode(Input::get('json'));
    
        // If the response was parseable, return it
        if($json !== null)
            return $json;
    
        // Determine if the response was a valid null or
        // why it was unparseable
        switch (json_last_error()) {
            // The server could respond with a valid null,
            // so go ahead and return it.
            case JSON_ERROR_NONE:
                return $json;
            case JSON_ERROR_DEPTH:
                echo ' - Maximum stack depth exceeded';
                break;
            case JSON_ERROR_STATE_MISMATCH:
                echo ' - Underflow or the modes mismatch';
                break;
            case JSON_ERROR_CTRL_CHAR:
                echo ' - Unexpected control character found';
                break;
            case JSON_ERROR_SYNTAX:
                echo ' - Syntax error, malformed JSON';
                break;
            case JSON_ERROR_UTF8:
                echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
                break;
            default:
                echo ' - Unknown error';
                break;
            }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器