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(...);
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
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.
While using Guzzle for async requests, I want got same callback immediately when each request done.
Following code is works, but I think it's may not make sense.
Does Guzzle provide any method like Promise\settle($promises)->then($callback)->wait() but for each requests get done?
<?php
require("vendor/autoload.php");
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
$client = new Client(['base_uri' => 'http://httpbin.org/']);
// I want got callback immediately when each request done.
$callback = function($response) {
echo $response->getBody();
};
$promises = [
$client->getAsync('/delay/8')->then($callback),
$client->getAsync('/delay/4')->then($callback),
$client->getAsync('/delay/2')->then($callback),
$client->getAsync('/delay/1')->then($callback),
];
Promise\settle($promises)->wait();
I am new to Guzzle and tried to read documentation, but still can't find the answer for it.
For example - I get this code from here
$request = $client->post('http://httpbin.org/post', array(), array(
'custom_field' => 'my custom value',
'file_field' => '#/path/to/file.xml'
));
$response = $request->send();
I tried to do the same thing, but when $client->post() is executed, it returns a response object, instead of request.
What can be wrong?
I am using the version 6.
As per Guzzle Docs all of the "magic methods", get(), delete(), put(), post(), options(), patch() and head() will return a response object.
If you check the GuzzleHttp\Client source code, you will see that the magic methods are actually abstractions to Client::request() handled by Client::__call().
Regardless of the request type, you should always receive a response.
I'm trying to use Zend_Soap_Client to communicate with an ASP.net web service. Here's my client call:
$client = new Zend_Soap_Client(null, array(
'location' => 'http://example.com/service.asmx',
'uri' => 'http://example.com/'
));
$user = new UserDetail();
$result = $client->UserDetails($user);
However this always gives me the error:
System.NullReferenceException: Object reference not set to an instance of an object. at Service.UserDetails(UserDetail UserDetail)
some googling revealed that this is quite a common problem. The most common solution seemed to be to pass the parameters as an array, so I tried:
$result = $client->UserDetails(array('UserDetail' => $user));
but this gave the same error. I also tried passing the params as a stdClass object, nesting the array in another with 'params' as the key, and a few other things but the error is always the same.
I have the ASP code for the web service itself, the relevant method is:
public Result UserDetails(UserDetail UserDetail) {
[some stuff]
Hashtable ht = new Hashtable();
ht = UserDetail.GenerateData();
}
the error is caused by the GenerateData() call.
I assume the UserDetails method is getting null instead of my object as the parameter, but I'm not sure how I should be calling the method, or how I can debug this further. The majority of the Zend_Soap_Client examples I've found seem to be using WSDL, which this service is not; not sure if that is relevant. Any help appreciated!
I eventually solved this with:
$userDetails = new UserDetails();
$userDetails->UserDetail = $user;
$client->UserDetails($userDetails);
it seems ASP.net expects (and returns) params to be nested in an object/array with the same name as the method being called.
If you have any possibility to change the asp.net code I'd suggest you try an implementation of the method UserDetails without parameters just to make sure that code isn't broken.
I would then create a consumer-method in asp.net, debug the http-request and see how the userdetail-object is serialized/broken down in array form. Then it's "just" a matter of creating a similar http request from php.