如何在 Laravel 项目中使用 guzzle
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47327164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use guzzle in Laravel Project
提问by santosh limbu thebe
I'm planning to use Guzzle to communicate with my XML web service. I am able to communicate with my XML web service using core PHP 5.6. I want to do this within my Laravel App using Guzzle to communicate with XML web service.
我计划使用 Guzzle 与我的 XML Web 服务进行通信。我能够使用核心 PHP 5.6 与我的 XML Web 服务进行通信。我想在我的 Laravel 应用程序中使用 Guzzle 与 XML Web 服务进行通信。
My xml config code is as follows:
我的xml配置代码如下:
$requestXmlBody ='';
$requestXmlBody = '<?xml version="1.0" encoding="UTF-8"?>';
$requestXmlBody = $requestXmlBody.'<FAB_PkgAvailRQ Target="test" Version="2002A" xmlns="https://localhost.com/find">';
$requestXmlBody = $requestXmlBody.'<SyndicatorInfo SyndicatorId="******" SyndicatorPassword="*****"/>';
$requestXmlBody = $requestXmlBody.'<SessionInfo CreateNewSession="true"/>';
$requestXmlBody = $requestXmlBody.'<HolidaySearchRequest ResponseTimeoutSecs="60" ExcludeNonBookableSuppliers="true">';
$requestXmlBody = $requestXmlBody.'<SearchCriteria FlightOnly="true" OneWayOnly="'.$journey_type.'" NumberOfAdults="'.$adults.'" NumberOfChildren="'.$children.'" NumberOfInfants="'.$infants.'">';
$requestXmlBody = $requestXmlBody.'<DepartureDateRange StartDate="'.$from.'" EndDate="'.$from.'"/>';
$requestXmlBody = $requestXmlBody.'<DepartureAirports><Airport>'.$origin.'</Airport></DepartureAirports>';
$requestXmlBody = $requestXmlBody.'<DestinationChoice><DestinationAirports><Airport>'.$destination.'</Airport></DestinationAirports></DestinationChoice>';
$requestXmlBody = $requestXmlBody.'<FlightPreferences/>';
$requestXmlBody = $requestXmlBody.'<HolidayDuration MinNumberOfNights="'.$ddays.'" MaxNumberOfNights="'.$ddays.'"/>';
$requestXmlBody = $requestXmlBody.'</SearchCriteria>';
// $requestXmlBody = $requestXmlBody.'<ResultSetPreferences MaxItems="'.$numberofsearch.'" SortCode="cost" SortAscending="true"/>';
// $requestXmlBody = $requestXmlBody.'<InitialViewInfo Length="'.$numberofsearch.'" Offset="0"/>';
$requestXmlBody = $requestXmlBody.'</HolidaySearchRequest>';
$requestXmlBody = $requestXmlBody.'</FAB_PkgAvailRQ>';
My curl code is as follows
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://localhost.com/23");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestXmlBody);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);
curl_close($ch);
$result1 = simplexml_load_string($result);
dd($result1);
I want to send a POST request using Guzzle to above xml configuration to my xml web service such as paxport/multicom. Any Idea How I can proceed?
我想使用 Guzzle 向上面的 xml 配置发送一个 POST 请求到我的 xml web 服务,例如 paxport/multicom。任何想法我该如何进行?
回答by Krishnamoorthy Acharya
Hi Please find the answer here
您好 请在这里找到答案
add it to composer file
将它添加到作曲家文件
"guzzlehttp/guzzle": "^6.2",
controller file
控制器文件
<?php
/** add it at top **/
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Guzzle\Http\Exception\ClientErrorResponseException;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Exception\BadResponseException;
/*** end ***/
use App\Http\Controllers\Controller as Controller;
class ApiController extends Controller {
function __construct() {
$this->client = new \GuzzleHttp\Client();
}
public function stockBatch($id) {
$apiKey = 'xyx';
$url = 'https://www.alphavantage.co/query?function=BATCH_STOCK_QUOTES&symbols=QQQ,AAPL&apikey=' . $apiKey;
$response = $this->client->get($url);
$results = $response->getBody();
$results = json_decode($results);
return response()->json($results);
}
}
回答by Alexey Shokov
It's simple.
这很简单。
use GuzzleHttp\Client;
$client = new Client();
$response = $client->post('http://localhost.com/23', ['body' => $requestXmlBody]);
$result = $response->getBody()->getContents();
$result1 = simplexml_load_string($result);