cURL request in Laravel

这一生的挚爱 提交于 2020-07-04 06:34:05

问题


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


回答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.




回答2:


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.




回答3:


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;
}



回答4:


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;

});


来源:https://stackoverflow.com/questions/48279382/curl-request-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!