Laravel中的cURL请求

kkbh8khc  于 2022-11-13  发布在  其他
关注(0)|答案(6)|浏览(447)

我正在努力在Laravel中发出此cURL请求

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

我一直在尝试这个:

$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();

但我收到错误Class 'App\Http\Controllers\GuzzleHttp\RequestOptions' not found
有什么建议吗?

编辑

我需要从$response中的API获取响应,然后将其存储在DB中......我如何才能做到这一点?:/

c0vxltue

c0vxltue1#

给予Guzzle中的query-option:

$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);

我使用这个选项来构建我的get-requests和guzzle。结合json_decode($json_values,true),你可以把json转换成php数组。

ct2axkht

ct2axkht2#

如果您在使用guzzlehttp:

原生Php方式

$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);

有时这个解决方案仍然比使用Laravel框架中附带的库更好和更简单。但仍然是你的选择,因为你掌握着你的项目的开发。

3df52oht

3df52oht3#

使用此代码作为参考。我已经成功地使用此代码进行了curl GET请求

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

izkcnapc4#

您忘记在名称空间前添加\。
您应该填写:

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

而不是:

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

gkl3eglg5#

我相信,自从Laravel 7以来,Laravel就有了一个HTTP客户端,这是一个Guzzle HTTP的 Package 器。

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
}

这是令人惊讶的,而且更干净,更容易测试,here's the documentation

wvt8vs2t

wvt8vs2t6#

使用Laravel,你可以写这样的东西在你的路线文件,如果你正在使用WP,你觉得冒险,不想使用狂饮或Laravel cURL包.

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

相关问题