Currently I only can send a get request to the url, but how do i safe the requested data to my database?:
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'https://xxxx.de/data');
echo $res->getStatusCode();
echo $res->getHeader('content-type')[0];
echo $res->getBody();
$request = new \GuzzleHttp\Psr7\Request('GET', 'https://xxxx.de/data');
$promise = $client->sendAsync($request)->then(function ($response) {
echo 'I completed! ' . $response->getBody();
});
$promise->wait();
Related
I am using laravel guzzle package for get response from this https://eos.greymass.com/v1/history/get_transaction url
$client = new Client();
try {
$response = $client->request('GET', 'https://eos.greymass.com/v1/history/get_transaction?id=18a20dbc34082451143c03ac54a2f24d06494d51e68f8fb1211ca0b63a53f37d');
}catch (ClientException $e) {
$response = $e->getResponse();
$responseBodyAsString = $response->getBody()->getContents();
return redirect()->back()->with('error', $responseBodyAsString);
}
if ($response->getStatusCode() != 200){
return redirect()->back()->with('error', 'Status code must be 200');
}
$body = $response->getBody();
return $body;
I get $body data properly, but when i tried to get $body->block_num then showing me this Undefined property: GuzzleHttp\Psr7\Stream::$block_num error
You have to decode the $response to get it as because it will convert json into an object
So for example:
$response = json_decode($client->request('GET', 'https://eos.greymass.com/v1/history/get_transaction?id=18a20dbc34082451143c03ac54a2f24d06494d51e68f8fb1211ca0b63a53f37d')->getBody(), true);
Try This! It will Help You
How to send a correct authorization header for basic authentication using guzzle
use GuzzleHttp\Client;
$username='EFDEMO';
$password='EFDEMO';
$client = new Client(['auth' => [$username, $password]]);
$res = $client->request('GET', 'https://mb-
rewards.calusastorefront.p2motivate.com/client/json.php/
getMemberAccount');
$res->getStatusCode();
$response = $res->getBody();
echo $response;
the error I am getting
{"statusCode":"Error","error":
{"errorCode":"400","errorMessage":"Authentication Header ID field must
match Basic Authentication Username"}}
Referring to documentation you should pass the auth parameter in request method instead of client's constructor:
$client = new Client();
$res = $client->request(
'GET',
'past-url-here',
['auth' => [$username, $password]]
);
I solved it like below
<?php
require '../x_static_libs/guzzle/vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();
$username = 'xxxxxx';
$password = 'xxxxxxxx';
$token = 'xxxxxxxxxxxxxxxxxxxx';
$url = 'https://ervb-rewards.com/client/json.php';
$api_token =
base64_encode(json_encode(['id'=>'xxxxxxx','token'=>'xxxxxxxxxxxxxxxxxxxxxx']));
$res = $client->request(
'GET',
$url.'/getMemberAccount',
['auth' => [$username,$api_token]]
);
$jsonData = json_decode($res->getBody(), true);
echo "<pre>";
print_r($jsonData);
echo "</pre>";
I'm trying to communicate to an API with laravel using guzzleHttp but I can't receive nothing.
If I test in postman returns me information.
In postman I set the configuration as you see in image:
In code I'm trying to do that:
$token = env('API_TOKEN');
$company = env('API_COMPANY_ID');
$link = 'https://xpto.pt/APIs.aspx/Con';
$data = array('token' => $token, 'empresa' => $company);
$data = json_encode($data);
echo $data;
$client = new Client();
$request = $client->post($link, ['raw'=>$data]);
$response = $request->send();
dd($response);
Thank you
i have this below code as:
public function postGuzzleRequest()
{
$client = new \GuzzleHttp\Client();
$url = "http://myexample.com/api/posts";
$myBody['name'] = "Demo";
$request = $client->post($url, ['body'=>$myBody]);
$response = $request->send();
dd($response);
}
now i'm trying to use that in async mode
taken directly from the doc:
http://docs.guzzlephp.org/en/stable/quickstart.html
$promise = $client->getAsync('http://httpbin.org/get');
$promise = $client->deleteAsync('http://httpbin.org/delete');
$promise = $client->headAsync('http://httpbin.org/get');
$promise = $client->optionsAsync('http://httpbin.org/get');
$promise = $client->patchAsync('http://httpbin.org/patch');
$promise = $client->postAsync('http://httpbin.org/post');
$promise = $client->putAsync('http://httpbin.org/put');
Here below i have attached my api request
$apiKey = "XXXX";
$Secret = "XXX";
$endpoint = "https://api.test.hotelbeds.com/hotel-api/1.0/hotels";
$request = new http\Client\Request("POST",
$endpoint,
[ "Api-Key" => $apiKey,
"X-Signature" => $signature,
"Accept" => "application/xml" ]);
try
{ $client = new http\Client;
$client->enqueue($request)->send();
$response = $client->getResponse();
if ($response->getResponseCode() != 200) {
printf("%s returned '%s' (%d)\n",
$response->getTransferInfo("effective_url"),
$response->getInfo(),
$response->getResponseCode()
);
} else {
printf($response->getBody());
}
} catch (Exception $ex) {
printf("Error while sending request, reason: %s\n",$ex->getMessage());
}'
getting following error
Uncaught Error: Class 'http\Client\Request' not found in
You need to add a use statement.
use http\Client\Request;
$request = new Request(blah blah);
Of course I assume you are using Composer autoloader. If not, you will also need to require_once() the file.
You can try using cURL instead of pecl_http. Here is an example:
<?php
// Your API Key and secret
$apiKey = "yourApiKey";
$Secret = "yourSecret";
// Signature is generated by SHA256 (Api-Key + Secret + Timestamp (in seconds))
$signature = hash("sha256", $apiKey.$Secret.time());
$endpoint = "https://api.test.hotelbeds.com/hotel-api/1.0/status";
echo "Your API Key is: " . $apiKey . "<br>";
echo "Your X-Signature is: " . $signature . "<br><br>";
// Example of call to the API
try
{
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $endpoint,
CURLOPT_HTTPHEADER => ['Accept:application/json' , 'Api-key:'.$apiKey.'', 'X-Signature:'.$signature.'']
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Check HTTP status code
if (!curl_errno($curl)) {
switch ($http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE)) {
case 200: # OK
echo "Server JSON Response:<br>" . $resp;
break;
default:
echo 'Unexpected HTTP code: ', $http_code, "\n";
echo $resp;
}
}
// Close request to clear up some resources
curl_close($curl);
} catch (Exception $ex) {
printf("Error while sending request, reason: %s\n",$ex->getMessage());
}
?>
Just make sure that first you uncomment ;extension=php_curl.dll in your php.ini file and restart your server.
We are planning to update our examples in the developer portal because some are outdated or not well documented.