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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配