org.springframework.http.converter.HttpMessageNotReadableException,无法读取文档:无法识别的标记

时间:2016-05-21 08:53:29

标签: php curl

我正在尝试向API发出POST请求。当我在邮递员中这样做时,一切都很完美。所以我很确定我的JSON是有效的。 当我尝试通过curl在PHP中执行POST请求时,会出现问题。所以我认为问题出在卷曲请求中。我在curl请求中使用与Postman相同的标题和正文。当我执行我的代码时,我收到以下错误:

<pre><pre class="xdebug-var-dump" dir="ltr">
<b>object</b>(<i>stdClass</i>)[<i>297</i>]
  <i>public</i> 'timestamp' <font color="#888a85">=&gt;</font> <small>string</small> <font color="#cc0000">'2016-05-20'</font> <i>(length=10)</i>
  <i>public</i> 'status' <font color="#888a85">=&gt;</font> <small>int</small> <font color="#4e9a06">400</font>
  <i>public</i> 'error' <font color="#888a85">=&gt;</font> <small>string</small> <font color="#cc0000">'Bad Request'</font> <i>(length=11)</i>
  <i>public</i> 'exception' <font color="#888a85">=&gt;</font> <small>string</small> <font color="#cc0000">'org.springframework.http.converter.HttpMessageNotReadableException'</font> <i>(length=66)</i>
  <i>public</i> 'message' <font color="#888a85">=&gt;</font> <small>string</small> <font color="#cc0000">'Could not read document: Unrecognized token 'skills': was expecting ('true', 'false' or 'null')
 at [Source: java.io.PushbackInputStream@74565e86; line: 1, column: 8]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'skills': was expecting ('true', 'false' or 'null')
 at [Source: java.io.PushbackInputStream@74565e86; line: 1, column: 8]'</font> <i>(length=376)</i>
  <i>public</i> 'path' <font color="#888a85">=&gt;</font> <small>string</small> <font color="#cc0000">'/task/solve'</font> <i>(length=11)</i>
</pre></pre>

错误说他希望('true','false'或'null')但实际上我需要发送一个数组。 在这里,您还可以找到我正在尝试执行的代码:

 public function sendDataToCreatePlanning($resource, $apiKey, $apiSecret, $smappyworldPlanningData) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_URL, 'https://serverpath/task/solve');

    // create header
    curl_setopt($ch, CURLOPT_HTTPHEADER,
     array("Authorization: Basic " . base64_encode($apiKey . ":" . $apiSecret),
            "Content-Type: application/json",
            "Accept: application/json",
           )
    );

    $arr =array(
            'solverType' => null,
            'solveTime' => null,
            'status' => null,
            'username' => null,
            //'hook' => null,
            'skills' => $smappyworldPlanningData['skills'],
            'employees' => $smappyworldPlanningData['active_users'],
            'dates' => null,
            'timeBlocks' => $smappyworldPlanningData['timeBlocks'],
            'tasks' => $smappyworldPlanningData['tasks'],
            'assignments' => $smappyworldPlanningData['assignments'],
            'blocksPerDay' => 95
            );

    //curl_setopt($ch, CURLOPT_USERAGENT, "XXX");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 
        http_build_query($arr));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    $json_payload = curl_exec($ch);

    if (!$json_payload) { echo curl_error($ch); }
    else {
        curl_close($ch);

        $dataAr = json_decode($json_payload);

        echo '<pre>';
        var_dump($dataAr);
        echo '</pre>';
        exit;

       echo "send data for planning succesfully!";

        return $dataAr;
    }
} 

1 个答案:

答案 0 :(得分:0)

如果这是一个休息api,那么你不应该去json_encode($arr)而不是http_build_query($arr)吗?就像这里:http://www.lornajane.net/posts/2011/posting-json-data-with-php-curl