我使用Guzzle在我的Symfony 3項目中執行來自外部API的HTTP GET請求。這裏是我的控制器代碼:
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Client;
class ScheduleController extends Controller {
/**
* @Route("/schedule")
*/
public function getJobs() {
// Create a client with a base URI
$client = new \GuzzleHttp\Client(['base_uri' => 'http://my.external.api/']);
// Send a request to http://my.external.api/site/67/module/1449/item
$response = $client->request('GET', 'site/67/module/1449/item', ['auth' => ['****', '****']]);
var_dump($response);
exit;
return $this->json(array($response));
}
}
我正在以下從我的代碼var_dump($response)
:
object(GuzzleHttp\Psr7\Response)#397 (6) { ["reasonPhrase":"GuzzleHttp\Psr7\Response":private]=> string(2) "OK" ["statusCode":"GuzzleHttp\Psr7\Response":private]=> int(200) ["headers":"GuzzleHttp\Psr7\Response":private]=> array(11) { ["Date"]=> array(1) { [0]=> string(29) "Tue, 24 Jan 2017 19:39:17 GMT" } ["Server"]=> array(1) { [0]=> string(6) "Apache" } ["Cache-Control"]=> array(2) { [0]=> string(35) "no-cache, no-store, must-revalidate" [1]=> string(46) "no-cache, no-store, max-age=0, must-revalidate" } ["Pragma"]=> array(2) { [0]=> string(8) "no-cache" [1]=> string(8) "no-cache" } ["Expires"]=> array(2) { [0]=> string(1) "0" [1]=> string(1) "0" } ["X-Content-Type-Options"]=> array(1) { [0]=> string(7) "nosniff" } ["X-XSS-Protection"]=> array(1) { [0]=> string(13) "1; mode=block" } ["X-Frame-Options"]=> array(1) { [0]=> string(4) "DENY" } ["Set-Cookie"]=> array(1) { [0]=> string(64) "SiteIdentifier=67; Expires=Wed, 25-Jan-2017 19:39:17 GMT; Path=/" } ["Transfer-Encoding"]=> array(1) { [0]=> string(7) "chunked" } ["Content-Type"]=> array(1) { [0]=> string(30) "application/json;charset=UTF-8" } } ["headerNames":"GuzzleHttp\Psr7\Response":private]=> array(11) { ["date"]=> string(4) "Date" ["server"]=> string(6) "Server" ["cache-control"]=> string(13) "Cache-Control" ["pragma"]=> string(6) "Pragma" ["expires"]=> string(7) "Expires" ["x-content-type-options"]=> string(22) "X-Content-Type-Options" ["x-xss-protection"]=> string(16) "X-XSS-Protection" ["x-frame-options"]=> string(15) "X-Frame-Options" ["set-cookie"]=> string(10) "Set-Cookie" ["transfer-encoding"]=> string(17) "Transfer-Encoding" ["content-type"]=> string(12) "Content-Type" } ["protocol":"GuzzleHttp\Psr7\Response":private]=> string(3) "1.1" ["stream":"GuzzleHttp\Psr7\Response":private]=> object(GuzzleHttp\Psr7\Stream)#395 (7) { ["stream":"GuzzleHttp\Psr7\Stream":private]=> resource(328) of type (stream) ["size":"GuzzleHttp\Psr7\Stream":private]=> NULL ["seekable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["readable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["writable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["uri":"GuzzleHttp\Psr7\Stream":private]=> string(10) "php://temp" ["customMetadata":"GuzzleHttp\Psr7\Stream":private]=> array(0) { } } }
當我通過Postman運行HTTP GET,我得到的結果,如:
"fields":[{"options":[{"id":23034,"value":"Ready for scheduling"}],"fieldDefinitionId":16444,"name":"Job Status"}
什麼是$response
我目前Ly gets,以及如何從我的外部API獲取我正在尋找的內容的JSON數組響應?
你可能會得到更多的如果您在提交更多內容之前回答有關您以前的問題的答案,請提供幫 – Cerad
什麼@Cerad?我對這個答覆感到困惑。 – Liz
在本網站上,在提出問題後,通常認爲對答案作出迴應是禮貌的。你昨天問了一個關於guzzle的問題,然後忽略了答覆。 – Cerad