2016-05-21 7 views
0

Я пытаюсь выполнить запрос POST в API. Когда я делаю это в Postman, все работает отлично. Поэтому я уверен, что мой JSON действителен. Проблема возникает, когда я пытаюсь выполнить запрос POST в PHP через curl. Поэтому я думаю, что проблема заключается в запросе на завивание. Я использую те же заголовки и тело в своем запросе на завивание, что и в Postman. Когда я исполню мой код, я получаю следующее сообщение об ошибке:org.springframework.http.converter.HttpMessageNotReadableException, не удалось прочитать документ: нераспознанный токен

<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: [email protected]; line: 1, column: 8]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'skills': was expecting ('true', 'false' or 'null') 
 
at [Source: [email protected]; 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>

ошибка говорит, что он ожидает («истина», «ложь» или «нулевой»), но на самом деле мне нужно отправить массив. Здесь вы также можете найти код, который я пытаюсь выполнить:

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; 
    } 
} 

ответ

Смежные вопросы