Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL request in Laravel

I am struggling to make this cURL request in Laravel

curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json"   -X GET http://my.domain.com/test.php 

I've been trying this:

$endpoint = "http://my.domain.com/test.php";  $client = new \GuzzleHttp\Client();  $response = $client->post($endpoint, [                 GuzzleHttp\RequestOptions::JSON => ['key1' => $id, 'key2' => 'Test'],             ]);  $statusCode = $response->getStatusCode(); 

But I am getting an error Class 'App\Http\Controllers\GuzzleHttp\RequestOptions' not found

Any suggestions?

EDIT

I need to get the response from API in $response and then store it in DB... How can I do this? :/

like image 391
harunB10 Avatar asked Jan 16 '18 10:01

harunB10


People also ask

What is cURL request in laravel?

Curl is another php http request tool for sending http requests, be it GET, POST, PUT etc. It is also used to integrate with web services. You may want to search for my other shot here on Edpresso on How to use Guzzle Http client request in Laravel.

Can we use cURL in laravel?

cUrl is mostly use in laravel when you work with any third party API. in laravel application cUrl is provide very easy way to functionality for call any API with any HTTP method. most off developer choose cUrl for call API as well as make any API.

What is cURL in laravel 8?

cURL is a tool to transfer data from or to a server, using one of the supported protocols (HTTP, HTTPS, FTP, FTPS, GOPHER, DICT, TELNET, LDAP or FILE).

What is cURL request?

'cURL' is a command-line tool that lets you transmit HTTP requests and receive responses from the command line or a shell script. It is available for Linux distributions, Mac OS X, and Windows. To use cURL to run your REST web API call, use the cURL command syntax to construct the command.


1 Answers

Give the query-option from Guzzle a try:

$endpoint = "http://my.domain.com/test.php"; $client = new \GuzzleHttp\Client(); $id = 5; $value = "ABC";  $response = $client->request('GET', $endpoint, ['query' => [     'key1' => $id,      'key2' => $value, ]]);  // url will be: http://my.domain.com/test.php?key1=5&key2=ABC;  $statusCode = $response->getStatusCode(); $content = $response->getBody();  // or when your server returns json // $content = json_decode($response->getBody(), true); 

I use this option to build my get-requests with guzzle. In combination with json_decode($json_values, true) you can transform json to a php-array.

like image 56
Brotzka Avatar answered Sep 21 '22 20:09

Brotzka