php Laravel 中的 cURL 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48279382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:39:29  来源:igfitidea点击:

cURL request in Laravel

phplaravelcurlguzzle

提问by harunB10

I am struggling to make this cURL request in Laravel

我正在努力在 Laravel 中提出这个 cURL 请求

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

但我收到一个错误 Class 'App\Http\Controllers\GuzzleHttp\RequestOptions' not found

Any suggestions?

有什么建议?

EDIT

编辑

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

我需要从 API 中获取响应$response,然后将其存储在 DB 中...我该怎么做?:/

回答by Brotzka

Give the query-option from Guzzle a try:

试试 Guzzle 的查询选项:

$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.

我使用这个选项来构建我的 get-requests with guzzle。与 json_decode($json_values, true) 结合使用,您可以将 json 转换为 php 数组。

回答by Kenneth Sunday

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

如果您在使用 guzzlehttp 时遇到问题,您仍然可以使用 PHP 中的原生 cURL:

Native Php way

原生 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);

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.

有时,这种解决方案比使用 Laravel 框架中附带的库更好、更简单。但仍然是您的选择,因为您拥有项目的开发权。

回答by Bishal Jung Chettri

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

以此作为参考。我已使用此代码成功发出 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;
}

回答by Bruce Tong

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.

使用 Laravel,如果您使用 WP 并且喜欢冒险并且不想使用 guzzle 或 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;

});