34

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? :/

harunB10
  • 4,823
  • 15
  • 63
  • 107
  • 2
    in one place you use \GuzzleHttp in other you use GuzzHttp (no backslash). Maybe that's an issue – matiit Jan 16 '18 at 10:48
  • Yeah, it is... But now I realized that I was trying to make `POST` request in Guzzle. I need to `GET` and store these values in DB. – harunB10 Jan 16 '18 at 10:49
  • Maybe you should take a look at the [query param option](http://docs.guzzlephp.org/en/stable/request-options.html?highlight=query#query). This options transforms given values to GET-parameters (key-value-based). – Brotzka Jan 16 '18 at 11:27

6 Answers6

59

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.

Wade
  • 3,757
  • 2
  • 32
  • 51
Brotzka
  • 2,959
  • 4
  • 35
  • 56
  • 1
    Try ```$response = $response->getBody();``` or ```json_decode($response->getBody(), true)``` when your server returns json. – Brotzka Jan 16 '18 at 11:38
20

You can still use the native cURL in PHP if you have trouble using guzzlehttp:

Native Php way

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "SOME_URL_HERE".$method_request);
// SSL important
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$output = curl_exec($ch);
curl_close($ch);


$this - > response['response'] = json_decode($output);

Sometimes this solution still better and simplier than using the library attached in the Laravel framework. But still your choice since you hold the development of your project.

Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
Kenneth Sunday
  • 865
  • 7
  • 14
5

Use this as reference . I have successfully made curl GET request with this code

public function sendSms($mobile)
{
  $message ='Your message';
  $url = 'www.your-domain.com/api.php?to='.$mobile.'&text='.$message;

     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_POST, 0);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

     $response = curl_exec ($ch);
     $err = curl_error($ch);  //if you need
     curl_close ($ch);
     return $response;
}
iman
  • 303
  • 1
  • 11
  • 27
5

I believe, since Laravel 7, Laravel comes with a HTTP Client which is a wrapper for Guzzle HTTP. So now something like this would have worked.

use Illuminate\Support\Facades\Http;


$response = Http::get('http://my.domain.com/test.php', [
    'key1' => $id, 
    'key2' => 'Test',
]);

if ($response->failed()) {
   // return failure
} else {
   // return success
}

it's amazing and much more cleaner, easier to test, here's the documentation

eballeste
  • 712
  • 1
  • 11
  • 23
  • 1
    For Laravel 8+, you must run `composer require guzzlehttp/guzzle` before you can use the Http wrapper in your code. (https://laravel.com/docs/9.x/http-client#introduction) – Udo E. Feb 21 '22 at 17:25
  • I think this answer should be the most accepted as it uses Laravel-specific functions. Thanks @eballeste for pointing this answer. – Eloy Ruiz Mar 16 '23 at 16:58
2

Using Laravel, you can write something like this in your routes file if you are using WP and you are feeling adventurous and don't want to use guzzle or Laravel cURL package.

Route::get('/curl',function() {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, 'https://example.net/wp-login.php');
    
    // save cookies to 'public/cookie.txt' you can change this later.
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

    curl_setopt($ch, CURLOPT_POSTFIELDS, ['log'=>'<name>','pwd'=>'<pass>']);

    curl_exec($ch);

    // supply cookie with request
    curl_setopt($ch, CURLOPT_COOKIE, 'cookie.txt');

    // the url you would like to visit
    curl_setopt($ch, CURLOPT_URL, 'https://example.net/profile/');

    $content = curl_exec($ch);

    curl_close($ch);

    // webpage will be displayed in your browser
    return;
});
ericmp
  • 1,163
  • 4
  • 18
Bruce Tong
  • 1,366
  • 15
  • 14
2

You have forgotten to add \ before namespace.

You should write:

$response = $client->post($endpoint, [
                \GuzzleHttp\RequestOptions::JSON => ['key1' => $id, 'key2' => 'Test'],
            ]);

Instead of:

$response = $client->post($endpoint, [
                GuzzleHttp\RequestOptions::JSON => ['key1' => $id, 'key2' => 'Test'],
            ]);