close

cURL request in Laravel

Hello Guys, How are you all? Hope You all Are Fine. Today we are talking about cURL request in Laravel in php. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How cURL request in Laravel Error Occurs?

Today we are talking about cURL request in Laravel in php.

How To Solve cURL request in Laravel Error ?

  1. How To Solve cURL request in Laravel Error ?

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

  2. cURL request in Laravel

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

Solution 1

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.

Solution 2

Using Laravel, you can write something like this in your routes file if your 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;

});

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read

Leave a Comment