本文整理汇总了PHP中Guzzle\Http\Message\Response::setBody方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::setBody方法的具体用法?PHP Response::setBody怎么用?PHP Response::setBody使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Guzzle\Http\Message\Response
的用法示例。
在下文中一共展示了Response::setBody方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: generateEbaySuccessResponse
/**
* @param $xml
* @return Response
*/
public function generateEbaySuccessResponse($xml)
{
$mockReponse = new Response(200);
if (file_exists($xml)) {
$mockReponse->setBody(EntityBody::factory(file_get_contents($xml)));
} else {
$mockReponse->setBody(EntityBody::factory($xml));
}
return $mockReponse;
}
示例2: testRetrieving
public function testRetrieving()
{
$response = new Response(200);
$response->setBody(json_encode(array('status' => 1, 'complete' => 1, 'list' => array(123 => array('item_id' => 123, 'resolved_id' => 123, 'given_url' => 'http://acairns.co.uk', 'given_title' => 'Andrew Cairns', 'favorite' => 0, 'status' => 0, 'time_added' => time(), 'time_updated' => time(), 'time_read' => 0, 'time_favorited' => 0, 'sort_id' => 0, 'resolved_title' => 'Andrew Cairns', 'resolved_url' => 'http://acairns.co.uk', 'excerpt' => 'Some excerpt about something', 'is_article' => 0, 'is_index' => 0, 'has_video' => 0, 'has_image' => 0, 'word_count' => 123)))));
$this->setPocketResponse($response);
$response = $this->pockpack->retrieve();
$this->assertEquals(1, $response->status);
$this->assertEquals(1, $response->complete);
$this->assertNotEmpty($response->list);
$this->assertNotEmpty($response->list->{123});
$item = $response->list->{123};
$this->assertEquals(123, $item->item_id);
$this->assertEquals(123, $item->resolved_id);
$this->assertEquals('Andrew Cairns', $item->given_title);
$this->assertEquals('Andrew Cairns', $item->resolved_title);
$this->assertEquals('http://acairns.co.uk', $item->given_url);
$this->assertEquals('http://acairns.co.uk', $item->resolved_url);
$this->assertEquals('Some excerpt about something', $item->excerpt);
$this->assertEquals(0, $item->is_article);
$this->assertEquals(0, $item->favorite);
$this->assertEquals(0, $item->status);
$this->assertEquals(0, $item->time_read);
$this->assertEquals(0, $item->time_favorited);
$this->assertEquals(0, $item->sort_id);
$this->assertEquals(0, $item->is_index);
$this->assertEquals(0, $item->has_video);
$this->assertEquals(0, $item->has_image);
$this->assertEquals(123, $item->word_count);
}
示例3: dispatch
public function dispatch($request, $connection)
{
$response = new Response(200);
$path = $request->getPath();
if ($this->match($path)) {
echo "Found match for path: {$path}" . PHP_EOL;
if (is_callable($this->routes[$path])) {
call_user_func_array($this->routes[$path], array($request, $response, $connection));
} else {
throw new \Exception('Invalid route definition.');
}
return;
}
if (is_null($this->fileHandler)) {
throw new \Exception('No file handler configured');
}
if (false === $this->fileHandler->canHandleRequest($request)) {
$response->setStatus(404);
$response->setBody('File not found');
$connection->send($response);
$connection->close();
return;
}
$this->fileHandler->handleRequest($request, $response, $connection);
}
示例4: onOpen
public function onOpen(ConnectionInterface $conn, RequestInterface $request = null)
{
$this->log('open', $conn);
$response = new Response(200, array('Content-type' => 'text/javascript', 'Content-Length' => $this->getFilesize($request->getPath())));
$response->setBody($this->getContent($request->getPath()));
$conn->send((string) $response);
$conn->close();
}
示例5: testReceivingAccessTokenAndUsername
public function testReceivingAccessTokenAndUsername()
{
$response = new Response(200);
$response->setBody(json_encode(array('access_token' => 'fake_access_token', 'username' => 'acairns')));
$this->setPocketResponse($response);
$data = $this->pockpack_auth->receiveTokenAndUsername('fake_consumer_key', 'fake_access_token');
$this->assertEquals('fake_access_token', $data['access_token']);
$this->assertEquals('acairns', $data['username']);
}
示例6: testCanCreateApiRateLimitErrorException
public function testCanCreateApiRateLimitErrorException()
{
$command = $this->getMock('Guzzle\\Service\\Command\\CommandInterface');
$request = $this->getMock('Guzzle\\Http\\Message\\Request', [], [], '', false);
$command->expects($this->once())->method('getRequest')->will($this->returnValue($request));
$response = new Response(400);
$response->setBody(json_encode(['error' => ['type' => 'invalid_request_error', 'code' => 'rate_limit']]));
$exception = BadRequestException::fromCommand($command, $response);
$this->assertInstanceOf('ZfrStripe\\Exception\\ApiRateLimitException', $exception);
}
示例7: it_can_create_the_exception
/** @test */
public function it_can_create_the_exception()
{
$command = $this->getMock('Guzzle\\Service\\Command\\CommandInterface');
$command->expects($this->once())->method('getRequest')->will($this->returnValue($this->getMock('Guzzle\\Http\\Message\\Request', [], [], '', false)));
$response = new Response(400);
$response->setBody('');
/** @noinspection PhpParamsInspection */
$exception = BadRequestException::fromCommand($command, $response);
$this->assertInstanceOf('Apache\\Usergrid\\Api\\Exception\\BadRequestException', $exception);
}
示例8: setUp
/**
* Setup
*/
public function setUp()
{
$plugin = new MockPlugin();
$response = new GuzzleResponse(200);
$response->setBody(file_get_contents(__DIR__ . '/Fixtures/response_projects.json'));
$plugin->addResponse($response);
$client = new VersionEyeClient('foo');
$client->addSubscriber($plugin);
$this->object = new VersionEyeApi($client, 'foo');
}
示例9: onOpen
public function onOpen(ConnectionInterface $conn, RequestInterface $request = null)
{
$this->emit('open', array($conn, $request));
try {
$this->router->dispatch($request, $conn);
} catch (Exception $e) {
$response = new Response(500);
$response->setBody("Server error.");
$conn->send($response);
$conn->close();
}
}
示例10: getClient
protected function getClient($fixtureFilePath = null)
{
if (null === $fixtureFilePath) {
return $this->getMock('Guzzle\\Http\\Client');
}
$response = new Response(200);
$response->setBody(file_get_contents($fixtureFilePath));
$mock = new MockPlugin();
$mock->addResponse($response);
$client = new Client();
$client->addSubscriber($mock);
return $client;
}
示例11: setUp
public function setUp()
{
$this->fixtureFilePath = __DIR__ . '/fixture';
$httpClient = $this->getMockBuilder('Guzzle\\Http\\Client')->setMethods(array('send'))->getMock();
// This client mock returns always a response object which body is a json-encoded array
// containing the url and the method of the request.
$httpClient->expects($this->any())->method('send')->will($this->returnCallback(function ($request) {
$response = new Response(200);
$response->setBody(json_encode(array('url' => $request->getUrl(), 'method' => $request->getMethod(), 'mime-type' => $request->getHeader('Content-Type', true))));
return $response;
}));
$this->client = new Client('test', 'user', 'pswd', $httpClient);
$this->client->setApiConfig($this->apiConfig);
}
示例12: testGetAllProjects
/**
* @covers \Wordsmith\Project::all
*/
public function testGetAllProjects()
{
$mockResponse = new Response(200);
$mockResponseBody = EntityBody::factory(fopen(__DIR__ . '/mock/bodies/projects.txt', 'r+'));
$mockResponse->setBody($mockResponseBody);
$mockResponse->setHeaders(array("Host" => "httpbin.org", "User-Agent" => "curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3", "Accept" => "application/json", "Content-Type" => "application/json"));
$plugin = new MockPlugin();
$plugin->addResponse($mockResponse);
$client = new HttpClient();
$client->addSubscriber($plugin);
$request = $client->get('https://api.automatedinsights.com/v1/projects');
$response = $request->send();
$this->assertEquals(200, $response->getStatusCode());
$this->assertTrue(in_array('Host', array_keys($response->getHeaders()->toArray())));
$this->assertTrue($response->hasHeader("User-Agent"));
$this->assertCount(4, $response->getHeaders());
$this->assertSame($mockResponseBody->getSize(), $response->getBody()->getSize());
$this->assertSame(1, count(json_decode($response->getBody(true))->data));
}
示例13: testDeserializeContextConfiguration
public function testDeserializeContextConfiguration()
{
$expectedContext = DeserializationContext::create();
$expectedContext->setGroups('group');
$expectedContext->setVersion(1);
$expectedContext->enableMaxDepthChecks();
$operation = $this->getMock('Guzzle\\Service\\Description\\OperationInterface');
$operation->expects($this->any())->method('getResponseType')->will($this->returnValue(OperationInterface::TYPE_CLASS));
$operation->expects($this->any())->method('getResponseClass')->will($this->returnValue('ResponseClass'));
$dataMap = array(array('jms_serializer.groups', 'group'), array('jms_serializer.version', 1), array('jms_serializer.max_depth_checks', true));
$operation->expects($this->any())->method('getData')->will($this->returnValueMap($dataMap));
$command = $this->getMock('Guzzle\\Service\\Command\\CommandInterface');
$command->expects($this->any())->method('getOperation')->will($this->returnValue($operation));
$response = new Response(200);
$response->setBody('body');
$serializer = $this->getMock('JMS\\Serializer\\SerializerInterface');
$serializer->expects($this->once())->method('deserialize')->with('body', 'ResponseClass', 'json', $this->equalTo($expectedContext));
$parser = new JMSSerializerResponseParser($serializer, $this->getMock('Guzzle\\Service\\Command\\ResponseParserInterface'));
$ref = new \ReflectionMethod($parser, 'deserialize');
$ref->setAccessible(true);
return $ref->invoke($parser, $command, $response, 'json');
}
示例14: fetch
public function fetch(RequestInterface $request)
{
$key = $this->getCacheKey($request);
if (!($entries = $this->cache->fetch($key))) {
return null;
}
$match = null;
$headers = $this->persistHeaders($request);
$entries = unserialize($entries);
foreach ($entries as $index => $entry) {
if ($this->requestsMatch(isset($entry[1]['vary']) ? $entry[1]['vary'] : '', $headers, $entry[0])) {
$match = $entry;
break;
}
}
if (!$match) {
return null;
}
// Ensure that the response is not expired
$response = null;
if ($match[4] < time()) {
$response = -1;
} else {
$response = new Response($match[2], $match[1]);
if ($match[3]) {
if ($body = $this->cache->fetch($match[3])) {
$response->setBody($body);
} else {
// The response is not valid because the body was somehow deleted
$response = -1;
}
}
}
if ($response === -1) {
// Remove the entry from the metadata and update the cache
unset($entries[$index]);
if ($entries) {
$this->cache->save($key, serialize($entries));
} else {
$this->cache->delete($key);
}
return null;
}
return $response;
}
示例15: createGuzzleMockPlugin
/**
* @return MockPlugin
*/
private function createGuzzleMockPlugin()
{
$terytPageFixturesPath = $this->fixturesPath . '/TerytPage';
$mock = new MockPlugin();
$downloadPageResponse = new Response(200);
$downloadPageResponse->setBody(file_get_contents($terytPageFixturesPath . DIRECTORY_SEPARATOR . 'listTerytFiles.html'));
$mock->addResponse($downloadPageResponse);
return $mock;
}