Is there a way using Guzzle in PHP that when I make a request to an API call that I can map my response to a Response object?
So instead of having to get the response data and then passing my array value as an argument, Guzzle can automatically resolve it to the required class?
In essence, this is what I am doing:
$client = new GuzzleHttp\Client();
$response = $client->request('myapi.users', 'GET');
$responseData = $response->getBody()->getContents();
$user = new User($responseData);
However I would like to try and avoid that boilerplate code by doing something like the following:
$client = new GuzzleHttp\Client();
$user = $client->request('myapi.users', 'GET');
Does Guzzle allow you to map response objects to Responses?
Thanks!
Nope, an HTTP Client (which Guzzle is) is not responsible for that. That's why there is not such a function there.
You can use Guzzle and your own object mapper, BTW, and create an SDK for the API you are using. Like the GitHub SDK, for example, that also uses Guzzle inside, but provides a specific interface for the domain.
Related
I need to perform post request using using Symfony framework. I can see there is package Symfony\Component\HttpFoundation\Request for this purpose. But when I create post request it seems doens't really perform request and return object data
$response = Request::create(get_api_url().'test','POST', $params);
How can I permorm real post request?
You can use
use Symfony\Component\HttpClient\HttpClient;
$client = HttpClient::create();
$response = $client->request('POST', 'https://...');
More details you can find here
Trying to make a call to the below web service, is this the correct way to pass a token in the soap header, its returning an incorrect token response, but the token is correct when i use it with soap ui..?
$soapclient = new SoapClient('http://api.fm-
web.co.za/webservices/AssetDataWebSvc/DriverProcessesWS.asmx?WSDL');
$token = array('Token'=>'XXXXXX');
$header = new SoapHeader('ass','soapenv',$token,false);
$response = $soapclient->__soapCall('GetDriverList',array(''),NULL,$header);
var_dump($response);
after some messing around this worked.
$soapclient = new SoapClient('http://api.fm-
web.co.za/webservices/AssetDataWebSvc/DriverProcessesWS.asmx?WSDL');
//$token = array('Token'=>'XXXXXX');
$header = new
SoapHeader('http://www.omnibridge.com/SDKWebServices/AssetData',
'TokenHeader',array('Token'=>'XXXXXX'),false);
//$DriverID = 3;
$params = array('DriverID'=>'3');
$response = $soapclient-
>__soapCall('GetDriver',array($params),NULL,$header);
var_dump($response);
SoapHeader definition can be tricky especially if you're not familiar with SOAP.
My advise would be to use a WSDL to PHP generator that would provide you a SDK with the method to set any SoapHeader and particularly this SoapHeader.
You should try the PackageGenerator project which should provide you the SoapHeader methods within the ServiceType\Get instance you'll have to create. Take a look to the generated tutorial.php file.
I would like to remove a specific cookie in a Guzzle response object.
My application uses Slim framework and I make calls to an API with Guzzle. Both Slim and Guzzle implement the Request and Response Interface (Psr7) so I can easily return a Guzzle response in a Slim controller like this :
class APIController {
public function call($request, $response) {
// Do stuff with $request (check body and params, change url, etc)
$client = new \GuzzleHttp\Client();
$response = $client->send($request, []);
return $response;
}
}
Everything works fine but the API returns a cookie I want to remove. I can remove the whole header with :
$response = $response->withoutHeader('Set-Cookie');
Is there a native way in Guzzle to remove a specific cookie by name instead of removing the whole header ?
I'm trying to send get request to this API https://api.coinmarketcap.com/v1/ticker/bitcoin/ and it's working fine, i'm getting the object but when i try to call object properties it's giving me error:
Undefined property: GuzzleHttp\Psr7\Response::$id
This is my code:
$client = new GuzzleHttp\Client(['base_uri' => 'https://api.coinmarketcap.com/v1/ticker/']);
$response = $client->request('GET', 'bitcoin');
return $response->id;;
I don't really know how to interact with this object...
The Guzzle Response object doesn't work that way, it doesn't assume what the response content is and proxy your request for a property.
You used to be able to call $response->json(), but you can't do that anymore due to PSR-7. Instead do something like this:
$items = json_decode($response->getBody());
foreach ($items as $item) {
echo($item->id);
}
That endpoint returns an array of objects. So you would need to either get the first one or loop through them if there are multiple.
NOTE: If you are adding the namespace at the top of your controller like:
use \GuzzleHttp\Client;
You then in your code need only to refer to it as Client like:
$client = new Client(...);
I need to query a WCF service in PHP, so that it can return an XML object back to me.
Previously I was able to do this using the http request and post method
$url = 'http://localhost:49000/';
//create the httprequest object
$httpRequest_OBJ = new httpRequest($url, HTTP_METH_POST, $options);
Using Soap. I can make a connection doing this:
// Create a new soap client based on the service's metadata (WSDL)
$client = new SoapClient("http://localhost:8731/FileUploadService?wsdl");
But how can I pass the XML object into the soap client and return a XML object.
If you really want to send a string containing the XML, you could use
$client->YourSoapMethodCall( new SoapVar($xmlString, XSD_ANYXML) ).
But it would be more convenient to feed parameters with an array or objects (which I use).
cf. http://andrecatita.com/code-snippets/php-soap-repeated-element-name/