我使用GuzzleHttp与本地休息API进行通信。但在某些情况下,答复是不完整的。缺少一些最后的字符。 json格式的答案,以及如何遗漏一些字符无法转换它。奇怪的是在浏览器中测试API,响应完成。
<?php
class A {
protected $client;
public function __construct()
{
$this->client = new \GuzzleHttp\Client([
'base_uri' => 'https://localhost:8443/restcomponent/notes/',
'timeout' => 2.0,
'verify' => false
]);
$this->transformer = new RestNoteTransformer();
}
/**
* @param string $httpMethod
* @param string $path
* @param array $options
* @return mixed|\Psr\Http\Message\ResponseInterface
*/
protected function request($httpMethod = 'GET', $path = '', array $options = [])
{
$options = array_merge($options, [
'headers' => [
'login.username' => 'admin',
'login.password' => 'ofbiz',
]
]);
return $this->client->request($httpMethod, $path, $options);
}
/**
* Find user notes
*
* @param int $userId
* @param int $page
* @return Collection
*/
public function getNotesByUser($userId, $page = 1)
{
$response = $this->request('GET', '?page=' . $page . '&nReg=' . self::NUM_REGS . '&user=' . $userId);
$contents = $response->getBody()->getContents();
$dto = json_decode($contents, false);
var_dump($contents, $dto);
}
}
回复示例:
[{"noteTitle":"EDOM: Trabalho: Entrega 1","lastUpdatedStamp":1452462860644,"createdTxStamp":1452462860642,"createdStamp":1452462860644,"_ENTITY_NAME_":"Note","lastUpdatedTxStamp":1452462860642,"noteId":"10038","Latitude":"41.101273400000004","media":"","userId":"1","Longitude":"-8.6503474","noteType":"text","text":"sdfjkjhg\r\nhjkkkhjgchjlkçkhj\r\nhvnklçljgbhjkkhghjkhhjllkhghj","_DELEGATOR_NAME_":"default"},{"noteTitle":"EDOM: Trabalho: Entrega 1","lastUpdatedStamp":1452462985812,"createdTxStamp":1452462985809,"createdStamp":1452462985812,"_ENTITY_NAME_":"Note","lastUpdatedTxStamp":1452462985809,"noteId":"10039","Latitude":"41.101273400000004","media":"","userId":"1","Longitude":"-8.6503474","noteType":"text","text":"sdfjkjhg\r\nhjkkkhjgchjlkçkhj\r\nhvnklçljgbhjkkhghjkhhjllkhghj","_DELEGATOR_NAME_":"default"},{"noteTitle":"EDOM: Trabalho: Entrega 1","lastUpdatedStamp":1452462992254,"createdTxStamp":1452462992250,"createdStamp":1452462992254,"_ENTITY_NAME_":"Note","lastUpdatedTxStamp":1452462992250,"noteId":"10040","Latitude":"41.101273400000004","media":"","userId":"1","Longitude":"-8.6503474","noteType":"text","text":"sdfjkjhg\r\nhjkkkhjgchjlkçkhj\r\nhvnklçljgbhjkkhghjkhhjllkhghj","_DELEGATOR_NAME_":"default"},{"noteTitle":"EDOM: Trabalho: Entrega 1","lastUpdatedStamp":1452463164627,"createdTxStamp":1452463164626,"createdStamp":1452463164627,"_ENTITY_NAME_":"Note","lastUpdatedTxStamp":1452463164626,"noteId":"10046","Latitude":"41.101273400000004","media":"","userId":"1","Longitude":"-8.6503474","noteType":"text","text":"sdfjkjhg\r\nhjkkkhjgchjlkçkhj\r\nhvnklçljgbhjkkhghjkhhjllkhghj","_DELEGATOR_NAME_":"default"},{"noteTitle":"EDOM: Trabalho: Entrega 1","lastUpdatedStamp":1452463249756,"createdTxStamp":1452463249755,"createdStamp":1452463249756,"_ENTITY_NAME_":"Note","lastUpdatedTxStamp":1452463249755,"noteId":"10047","Latitude":"41.101273400000004","media":"","userId":"1","Longitude":"-8.6503474","noteType":"text","text":"sdfjkjhg\r\nhjkkkhjgchjlkçkhj\r\nhvnklçljgbhjkkhghjkhhjllkhghj","_DELEGATOR_NAME_":"default"},{"noteTitle":"EDOM: Trabalho: Entrega 1","lastUpdatedStamp":1452463284029,"createdTxStamp":1452463284027,"createdStamp":1452463284029,"_ENTITY_NAME_":"Note","lastUpdatedTxStamp":1452463284027,"noteId":"10048","Latitude":"41.101273400000004","media":"","userId":"1","Longitude":"-8.6503474","noteType":"text","text":"sdfjkjhg\r\nhjkkkhjgchjlkçkhj\r\nhvnklçljgbhjkkhghjkhhjllkhghj","_DELEGATOR_NAME_":"default"},{"noteTitle":"EDOM: Trabalho: Entrega 1","lastUpdatedStamp":1452463309824,"createdTxStamp":1452463309822,"createdStamp":1452463309824,"_ENTITY_NAME_":"Note","lastUpdatedTxStamp":1452463309822,"noteId":"10049","Latitude":"41.101273400000004","media":"","userId":"1","Longitude":"-8.6503474","noteType":"text","text":"sdfjkjhg\r\nhjkkkhjgchjlkçkhj\r\nhvnklçljgbhjkkhghjkhhjllkhghj","_DELEGATOR_NAME_":"default"},{"noteTitle":"Retrieve data from webservice","lastUpdatedStamp":1452463392692,"createdTxStamp":1452463392690,"createdStamp":1452463392692,"_ENTITY_NAME_":"Note","lastUpdatedTxStamp":1452463392690,"noteId":"10050","Latitude":"41.1012888","media":"","userId":"1","Longitude":"-8.6503467","noteType":"text","text":"kjhfcvghjkm\r\njnbhjmlmkh\r\nnhbhjkjhv","_DELEGATOR_NAME