Laravel中的cURL请求 [英] cURL request in Laravel

查看:1478
本文介绍了Laravel中的cURL请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我一直在尝试:

$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

有任何建议吗?

编辑

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

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

推荐答案

尝试从Guzzle提供查询选项:

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

我使用这个选项来建立我的获取请求。结合json_decode($ json_values,true),您可以将json转换为php-array。

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.

这篇关于Laravel中的cURL请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆