I have the following code in CakePHP. I need the same code in laravel using guzzle
$url = "https://xyz?";
$query ='first_name='. $data->FirstName .'&gender=""'. '&home_phone='. $data->HomePhone.'&ip_address='. $data->IPAddress.'&last_name='. $data->LastName.'&user_defined_url='. $result;
$HttpSocket = new HttpSocket(array('ssl_verify_peer' => false, 'ssl_verify_host' => false));
$post_response = $HttpSocket->get($url,$query);
$response = explode('&',$post_response->body);
I have converted it in laravel using guzzle but doesnt work. Following my code that Ive converted in laravel:
$client = new Client(['verify' => false ]);
$post_response = $client->get($url, $query);
$response = explode('&',$post_response->body);
Note: use GuzzleHttp\Client; is written at the top of file.
Thanks in advance for the help!
Can you try like this,
$client = new Client();
$post_response = $client->request('GET', $url, [
'verify' => false,
'form_params' => [
'first_name' => $data->FirstName,
'gender' => "",
'home_phone' => $data->HomePhone,
'ip_address' => $data->IPAddress,
'last_name' => $data->LastName,
'user_defined_url' => $result
]
]);
$response = explode('&',$post_response->body);
Instead of form_params you can use query for sending the parameters as query string.
Related
I want to send a request to external API using guzzle, but not foreach is not running.
public function recursub() {
$usersCheck = User::where('trialExpires', '<=', Carbon::now());
//Get all Check User
foreach ($usersCheck as $user) {
dd('Hello');
$url = 'https://api.##############';
$client = new Client();
$response = $client->request('GET', $url, [
'headers' => [
'Authorization' => 'Bearer '.'#########################',
'Content-Type' => 'application/json'
],
'form_params' => [
'authorization_code' => $user->authorization_code,
'customer' => $user->email,
//'plan' => '#######################',
]
]);
It worked if I hardcode the value into the form paramas.
public function recursub() {
$usersCheck = User::where('trialExpires', '<=', Carbon::now())->get();
//Get all Check User
foreach ($usersCheck as $user) {
dd('Hello');
$url = 'https://api.##############';
$client = new Client();
$response = $client->request('GET', $url, [
'headers' => [
'Authorization' => 'Bearer '.'#########################',
'Content-Type' => 'application/json'
],
'form_params' => [
'authorization_code' => $user->authorization_code,
'customer' => $user->email,
//'plan' => '#######################',
]
]);
get() method is used to get data in array, so now you have to add get() method in the last of the query. Here is the code below:
$usersCheck = User::where('trialExpires', '<=', Carbon::now())->get();
get() method is used for getting multiple records and first is used for single record.
i am trying to create SSO URL for the paperlesspipeline, so it no needs to credential for login
i have to use laravel 5.8 and it's GuzzleHttp package
$timestamp = date(DateTime::ISO8601);
$userID = '*****';
$url = "https://dev.paperlesspipeline.com/sso/signin-v1/";
$hash = hash_hmac('SHA256', $userID.$timestamp,secret key,false);
$client = new \GuzzleHttp\Client(['http_errors' => false]);
$options = [
'form_params' => [
"USERID" => $userID,
"timestamp" => $timestamp,
"hash" => $hash
]
];
$response = $client->post($url, $options);
I am not get sso url, can you halp me what wrong.
You can try this
$response = $client->request('POST', $url, $options);
So i am messing about with the Marvel API and i have been getting this error and i cannot find any trace of it anywhere.
`409 Conflict` response: {"code":"MissingParameter","message":"You must provide a user key."}
I have checked through the API Documentation and i cannot find anything about a user key.
Here is my code; I am using Laravel with Guzzle.
$res = $client->request('GET', 'http://gateway.marvel.com:80/v1/public/comics', [
'apikey' => $apikey,
'ts' => $now,
'hash' => md5($now . $privateKey . $apikey),
]);
Any help would be greatly appreciated.
Try using http_build_query:
$query = http_build_query([
'apikey' => $apikey,
'ts' => $now,
'hash' => md5($now . $privateKey . $apikey)
]);
$url = 'http://gateway.marvel.com:80/v1/public/comics?' . $query;
$res = $client->request('GET', $url);
update
Looks like you just need to set the query option in the request.
$res = $client->request('GET', 'http://gateway.marvel.com:80/v1/public/comics', [
'query' => [
'apikey' => $apikey,
'ts' => $now,
'hash' => md5($now . $privateKey . $apikey)
]
]);
Changed the upload folder, the problem is solved.
how to pass path parameter in guzzle HTTP Post request. I am having url like this - http://base_url/v1/rack/{id}/books
in my url {id} is the path parameter.
$addLibraryUrl = $base_url."v1/rack/{id}/book";
$headers["id"] = $id;
$requestContent['json'] = $data;
$client = new Client();
$response = $client->post($addLibraryUrl, [
"headers" => $headers,
"json" => json_encode($data)
]);
I dont think the latest guzzle uses URI templates, but the functionality to do the parameter replacing is still there:
$addLibraryUrl = \GuzzleHttp\uri_template($base_url. "v1/rack/{id}/book" , [
'id' => $id,
]);
Also you can just put the id into the URI yourself very easily.
$addLibraryUrl = $base_url."v1/rack/{$id}/book";
example:
blah.com/v1/rack/5/book
I solved my issue in this way:
$id = your_rack_id_value;
$client = new Client([ 'base_uri' => $base_url, ]);
$uri = 'v1/rack/.$id.'/book';
$requestEncodedData = json_encode($data);
$response = $client->post($uri, [
'body' => $requestEncodedData,
'headers' => [
'Content-Type' => 'application/json',
]
]);
If you sent form params then you need to sent them as post params like :
// Initialize Guzzle client
$client = new GuzzleHttp\Client(['headers'=> 'Some headers']);
// Create a POST request
$response = $client->request(
'POST',
'http://yoururl.com',
[
'form_params' => [
'key1' => 'value1',
'key2' => 'value2'
]
]
);
Or like in your case change 'json' to form_params:
$addLibraryUrl = $base_url."v1/rack/{id}/book";
$headers["id"] = $id;
$requestContent['json'] = $data;
$client = new Client();
$response = $client->post($addLibraryUrl, [
"headers" => $headers,
"form_params" => json_encode($data)
]);
I need to integrate an API so I write function:
public function test() {
$client = new GuzzleHttp\Client();
try {
$res = $client->post('http://example.co.uk/auth/token', [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'json' => [
'cliend_id' => 'SOMEID',
'client_secret' => '9999jjjj67Y0LBLq8CbftgfdreehYEI=',
'grant_type' => 'client_credentials'
]
]);
$res = json_decode($res->getBody()->getContents(), true);
dd($res);
}
catch (GuzzleHttp\Exception\ClientException $e) {
$response = $e->getResponse();
$result = json_decode($response->getBody()->getContents());
return response()->json(['data' => $result]);
}
}
as a responde I got message:
{"data":{"error":"invalid_clientId","error_description":"ClientId should be sent."}}
Now when I try to run the same url with same data in POSTMAN app then I get correct results:
What is bad in my code? I send correct form_params also I try to change form_params to json but again I got the same error...
How to solve my problem?
The problem is that in Postman you're sending the data as a form, but in Guzzle you're passing the data in the 'json' key of the options array.
I bet that if you would switch the 'json' to 'form_params' you would get the result you're looking for.
$res = $client->post('http://example.co.uk/auth/token', [
'form_params' => [
'client_id' => 'SOMEID',
'client_secret' => '9999jjjj67Y0LBLq8CbftgfdreehYEI=',
'grant_type' => 'client_credentials'
]
]);
Here's a link to the docs in question: http://docs.guzzlephp.org/en/stable/quickstart.html#sending-form-fields
Also, I noticed a typo - you have cliend_id instead of client_id.