【问题标题】:cURL request in LaravelLaravel 中的 cURL 请求
【发布时间】:2018-06-25 01:14:16
【问题描述】:

我正在努力在 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 获取响应,然后将其存储在数据库中……我该怎么做? :/

【问题讨论】:

  • 在一个地方你使用 \GuzzleHttp 在另一个地方你使用 GuzzHttp (没有反斜杠)。也许这是个问题
  • 是的,它是......但现在我意识到我试图在 Guzzle 中发出POST 请求。我需要GET 并将这些值存储在数据库中。
  • 也许你应该看看query param option。此选项将给定值转换为 GET 参数(基于键值)。

标签: php laravel curl guzzle


【解决方案1】:

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

我使用此选项通过 guzzle 构建我的 get-requests。结合 json_decode($json_values, true) 可以将 json 转换为 php-array。

【讨论】:

  • 当您的服务器返回 json 时尝试$response = $response->getBody();json_decode($response->getBody(), true)
【解决方案2】:

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

原生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 框架中附加的库更好、更简单。但仍然是您的选择,因为您拥有项目的开发权。

【讨论】:

  • 我在使用 curl 时出错:调用未定义的函数 App\Http\Controllers\curl_init() 我正在使用 Laravel 8
【解决方案3】:

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

【讨论】:

    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      您忘记在命名空间前添加 \。

      你应该写:

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

      代替:

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

      【讨论】:

        【解决方案6】:

        我相信,从 Laravel 7 开始,Laravel 带有一个 HTTP 客户端,它是 Guzzle HTTP 的包装器。所以现在这样的事情会奏效。

        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

        【讨论】:

          猜你喜欢
          • 2015-01-20
          • 2021-02-21
          • 1970-01-01
          • 2019-12-17
          • 1970-01-01
          • 2019-03-27
          • 2017-04-04
          • 2019-04-08
          • 1970-01-01
          相关资源
          最近更新 更多