【问题标题】:Could not parse the 'params' argument as valid JSON (PHP, cURL, REST)无法将“参数”参数解析为有效的 JSON(PHP、cURL、REST)
【发布时间】:2016-09-13 14:34:56
【问题描述】:

执行 PHP 脚本后,我收到以下错误 - 感谢任何帮助。根据我的理解,我根据 bugzilla 文档创建了一个数组 - 提前致谢

https://bugzilla.readthedocs.io/en/5.0/api/core/v1/bug.html#create-bug

{"code":32000,"error":true,"message":"无法将 'params' 参数解析为有效的 JSON。错误:数字格式错误(初始减号后没有数字),字符偏移量 1

       $url ="http://localhost:8080/bugzilla/rest/bug";

       $data = array(
            "product" => "TestProduct",
            "component" => "TestComponent",
            "version" => "unspecified",
            "summary" => "'This is a test bug - please disregard",
            "alias" => "SomeAlias",
            "op_sys" => "All",
            "priority" => "P1",
            "rep_platform" => "All"
        );

        $str_data = json_encode($data);

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
        curl_setopt($ch, CURLOPT_HTTPHEADER, 
                array("Content-Type: application/json", "Accept: application/json")); 
        $username = 'ashish.sureka@in.abb.com';
        $password = 'incrc';
        curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
        $result = curl_exec($ch);
        curl_close($ch); 

        echo $result

【问题讨论】:

  • 当您使用键 summary$data 数组中的值中删除单引号时,是否仍然会出现这种情况?
  • 感谢您的关注。我删除了单引号 ' 但仍然遇到同样的错误
  • 您不应该将$str_data 用于您的 POSTFIELDS 参数吗?

标签: php json rest


【解决方案1】:

你有这个:

    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);

当你应该有的时候,它将你的数据数组作为传统的key=value 表单提交发送

    curl_setopt($ch, CURLOPT_POSTFIELDS, $str_data);
                                         ^^^^^

这将发送您的 JSON 编码数组。

【讨论】:

    【解决方案2】:

    您在 CURLOPT_POSTFIELDS 中发送 $data 但我认为您需要给它 $str_data 以 JSON 编码

    【讨论】:

      【解决方案3】:

      我认为你必须更换

      curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
      

      curl_setopt($ch, CURLOPT_POSTFIELDS, $str_data);
      

      传输 json 编码的数组而不是原始的 php 数组。

      【讨论】:

        猜你喜欢
        • 2011-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-12
        • 1970-01-01
        • 1970-01-01
        • 2017-09-26
        相关资源
        最近更新 更多