Sending variables to another application and get the response from that? - php

I need to send data to another webpage of different application and it will send some json data which i need to use in further instruction.
I need to send some basic information like cus_name, cus_email, cus_phone to that webpage and that will send some data as json format.
i got the basic idea of how can i catch the json response : like that,
$client = new Client();
$body = $client->get('https://securepay.google.com/gwprocess/v3/api.php')->getBody();
$data = json_decode($body);
return redirect($data->GatewayPageURL);
How do i send those variables to and catch the response in same controller?
Thanks in advance.

You can send Query String Parameters in two ways.
Include them in the URI itself.
$response = $client->request('GET', 'https://securepay.google.com/gwprocess/v3/api.php?cus_name=name&cus_email=email&cus_phone=phone');
Or
Specify them using the query request option as an array
$response = $client->request('GET', 'https://securepay.google.com/gwprocess/v3/api.php', [
'query' => [
'cus_name' => 'name',
'cus_email' => 'email',
'cus_phone' => 'phone'
]
]);
Or for Post Requests:
$response = $client->request('POST', 'https://securepay.google.com/gwprocess/v3/api.php', [
'form_params' => [
'cus_name' => 'name',
'cus_email' => 'email',
'cus_phone' => 'phone'
]
]);
and then to get the response:
$result = json_decode($response->getBody());

Related

Guzzle with Laravel 9

I'm working on Lavravel 9 with guzzle to send some data in
the below code need to get some data from the database how can get data from the database?.
$response = Http::post('APIurl', [
"headers" => [
//header information
],
"body" => [
'title' => "**Get data from database**",
'body' => "**Get data from database**,
'userId' => 22
],
]);
Thank you
Assuming your API returns a json result, simply call the json() method on the Response object. If you want the raw response body, use body() instead.
For example, take github's api:
$response = Http::get('api.github.com'); // replace with your call
$body = $response->body(); // returns string
$json = $response->json(); // tries to return a json array.
To add data to your request, try passing a json string in the body.
$data = json_encode([
'title' => ...,
'body' => ...,
'user_id' => ...,
]);
$response = Http::post('your-api-here', [
'headers' => [
...
],
'body' => $data,
]);

Failed to post data using Guzzle 6

I've tried dozens of times to debug this, but I can't find where my error code. The problem is simple, I just want to post data with the application/json format
$this->client = new GuzzleHttp\Client(['base_uri' => 'https://myendpointapi.com/']);
$headers = [
'Authorization' => $auth,
'Accept'=>'application/json',
'Content-Type' => $contentTypes
];
$url = "my-service";
$data = ["foo" => "bar"];
$this->client->send('POST',$url,["headers" => $headers, "json" => $data ]);
but the error I cant post $data, the response inform me no data send by me. I am also try to change $data value like
$data = array(["foo" => "bar"])
or change the format to multipart request (application/x-www-form-urlencoded) but the response keep same. I don't know why my code is not send the data.

Retrieve POST data sent with Guzzle (6.3) from Laravel

I am posting some data from my laravel app to another plain PHP page I have on my local server. The status code returned by Guzzle says it was successful but I am unable to access it. How can I do this?
I've tried using the Post method to retrieve value but no luck
Guzzle Function:
$client = new Client();
$response = $client->request('POST', 'http://localhost/testApp/guzTest.php',
[
'form_params' => [
'amnt' => 75
],
'allow_redirects' => true
]);
echo $response->getStatusCode();
Page Receiving POST DATA:
<?php
if (!empty($_POST['amnt'])) {
echo $_POST['amnt'];
}else
echo 'not found';
?>
I expect to be able to access the amount posted via post method but nothing is yielded.
Try somthing like that .
$client = new Client();
$response = $client->request('POST', 'http://localhost/testApp/guzTest.php',
[
'form_params' => [
'amnt' => 75
],
'allow_redirects' => true
]);
$contents = $response->getBody()->getContents();
$contents = json_decode($contents,true);
return ($contents);
On page Receiving POST DATA:
Response should be like this
return response()->json([
'status' => 'SUCCESS',
'code' => '200',
], 200);

Receive data in Json in PHP

I am posting data to my script file to receive data. In my script file, File.php, I am not able to get the object patient in the dumped results. When i do var_dump($get_patient_info->patient);,
it throws an error saying Object {patient} not found.
Could i be mapping the data wrongly?
PS: Beginner in Laravel
SendingData Controller
$hospitalData = [];
$hospitalData[] = [
'patient' => 'Mohammed Shammar',
'number' => '34',
],
$url = "https://example.com/file.php";
$client = new Client();
$request = $client->post($url, [
'multipart' => [
[
'name' => 'patient_info',
'contents' => json_encode($hospitalData),
],
],
]);
$response = $request->getBody();
return $response;
File.php
$get_patient_info = $_POST['patient_info'];
var_dump($get_patient_info);
Results
string(189) "[{"patient":"Mohammed Shammar","number":"34"}]"
You can json_decode and fetch the data as follows,
$temp = json_decode($get_patient_info);
echo $get_patient_info[0]->patient;
json_decode — Decodes a JSON string
Hope this helps.

guzzle ver 6 post method is not woking

is working in postman (raw format data with with application/json type)
with guzzle6
url-http://vm.xxxxx.com/v1/hirejob/
{
"company_name":" company_name",
"last_date_apply":"06/12/2015",
"rid":"89498"
}
so am getting response 201 created
but in guzzle
$client = new Client();
$data = array();
$data['company_name'] = "company_name";
$data['last_date_apply'] = "06/12/2015";
$data['rid'] = "89498";
$url='http://vm.xxxxx.com/v1/hirejob/';
$data=json_encode($data);
try {
$request = $client->post($url,array(
'content-type' => 'application/json'
),array());
} catch (ServerException $e) {
//getting GuzzleHttp\Exception\ServerException Server error: 500
}
i am getting error on vendor/guzzlehttp/guzzle/src/Middleware.php
line no 69
? new ServerException("Server error: $code", $request, $response)
You're not actually setting the request body, but arguably the easiest way to transfer JSON data is by using the dedicated request option:
$request = $client->post($url, [
'json' => [
'company_name' => 'company_name',
'last_date_apply' => '06/12/2015',
'rid' => '89498',
],
]);
You need to use json_encode() JSON_FORCE_OBJECT flag as the second argument. Like this:
$data = json_encode($data, JSON_FORCE_OBJECT);
Without the JSON_FORCE_OBJECT flag, it will create a json array with bracket notation instead of brace notation.
Also, try sending a request like this:
$request = $client->post($url, [
'headers' => [ 'Content-Type' => 'application/json' ],
'body' => $data
]);

Categories