/** * 检测execute功能是否正常 */ public function testExecute() { $request = Request::factory('http://www.baidu.com'); //$request->method = Http::POST; $request->query(['v1' => 'v2']); //$request->post(['v3' => 'v4']); $response = $request->execute(); $this->assertTrue(strpos($response->body, '百度') !== false); }
/** * 跳转状态码的处理 * * @param Request $request * @param Response $response * @param RequestClient $client * @return null|Request */ public static function onHeaderLocation(Request $request, Response $response, RequestClient $client) { if ($client->follow && in_array($response->status, [Http::CREATED, Http::MOVED_PERMANENTLY, Http::FOUND, Http::SEE_OTHER, Http::TEMPORARY_REDIRECT])) { switch ($response->status) { default: case Http::MOVED_PERMANENTLY: case Http::TEMPORARY_REDIRECT: $followMethod = $request->method; break; case Http::CREATED: case Http::SEE_OTHER: $followMethod = Http::GET; break; case Http::FOUND: if ($client->strictRedirect) { $followMethod = $request->method; } else { $followMethod = Http::GET; } break; } $origHeaders = $request->headers(); $followHeaders = array_intersect_assoc($origHeaders, array_fill_keys($client->followHeaders, true)); $followRequest = Request::factory($response->headers('Location')); $followRequest->method = $followMethod; $followRequest->headers($followHeaders); if ($followMethod !== Http::GET) { $followRequest->body = $request->body; } return $followRequest; } return null; }