What I want is get an object from an API with a HTTP (eg, jQuery's AJAX) request to an external api. How do I start? I did research on Mr Google but I can't find anything helping.
我想要的是从带有HTTP(例如jQuery的AJAX)请求的API中获取一个对象到一个外部API。我如何开始?我对谷歌先生做了研究,但没有发现任何有用的东西。
Im starting to wonder is this is even possible? In this post Laravel 4 make post request from controller to external url with data it looks like it can be done. But there's no example nor any source where to find some documentation.
我开始怀疑这是否可能?在这个post Laravel 4中,将post请求从控制器发送到带有数据的外部url,看起来是可以完成的。但是没有例子,也没有任何来源可以找到文档。
Please help me out?
请帮助我吗?
6 个解决方案
#1
111
Based upon an answer of a similar question here: https://*.com/a/22695523/1412268
基于一个类似问题的答案:https://*.com/a/22695523/1412268
Take a look at Guzzle
看看Guzzle
$client = new GuzzleHttp\Client();
$res = $client->get('https://api.github.com/user', ['auth' => ['user', 'pass']]);
echo $res->getStatusCode(); // 200
echo $res->getBody(); // { "type": "User", ....
#2
63
We can use package Guzzle in Laravel, it is a PHP HTTP client to send HTTP requests.
我们可以使用Laravel的包Guzzle,它是一个PHP HTTP客户端来发送HTTP请求。
You can install Guzzle through composer
您可以通过composer安装Guzzle
composer require guzzlehttp/guzzle:~6.0
Or you can specify Guzzle as a dependency in your project's existing composer.json
或者,您可以在项目的现有组合.json中指定Guzzle作为依赖项
{
"require": {
"guzzlehttp/guzzle": "~6.0"
}
}
Example code in laravel 5 using Guzzle as shown below,
laravel 5中使用Guzzle的示例代码如下所示,
use GuzzleHttp\Client;
class yourController extends Controller {
public function saveApiData()
{
$client = new Client();
$res = $client->request('POST', 'https://url_to_the_api', [
'form_params' => [
'client_id' => 'test_id',
'secret' => 'test_secret',
]
]);
echo $res->getStatusCode();
// "200"
echo $res->getHeader('content-type');
// 'application/json; charset=utf8'
echo $res->getBody();
// {"type":"User"...'
}
#3
33
You just want to call an external URL and use the results? PHP does this out of the box, if we're talking about a simple GET request to something serving JSON:
您只想调用外部URL并使用结果?PHP是开箱即用的,如果我们讨论的是对服务JSON的对象的简单GET请求:
$json = json_decode(file_get_contents('http://host.com/api/stuff/1'), true);
If you want to do a post request, it's a little harder but there's loads of examples how to do this with curl.
如果你想做一个post请求,这有点难,但是有很多关于如何使用curl的例子。
So I guess the question is; what exactly do you want?
我想问题是;你到底想要什么?
#4
12
If you just need to fire the request to external API, you can use Laravel Request class as follows(Assuming you use GET request)
如果您只需要向外部API发出请求,您可以使用Laravel请求类,如下所示(假设您使用的是GET请求)
$request = Request::create('http://your-api.com', 'GET');
But if you need to get the content of the response, You can use Laracurl package as follows:
但是如果您需要获取响应的内容,可以使用Laracurl包,如下所示:
$response = Laracurl::get('http://your-api.com');
#5
2
You can make requests using Laravel and without external packages
您可以使用Laravel和没有外部包进行请求。
$request = \Illuminate\Http\Request::create('http://your-api.com', 'POST', ['param1' => 'value1', 'param2' => 'value2']);
Request functionality is provided because Laravel relies on Symfony's Request package.
之所以提供请求功能,是因为Laravel依赖于Symfony的请求包。
#6
1
You can use Httpful :
您可以使用Httpful:
Website : http://phphttpclient.com/
网站:http://phphttpclient.com/
Github : https://github.com/nategood/httpful
Github:https://github.com/nategood/httpful
#1
111
Based upon an answer of a similar question here: https://*.com/a/22695523/1412268
基于一个类似问题的答案:https://*.com/a/22695523/1412268
Take a look at Guzzle
看看Guzzle
$client = new GuzzleHttp\Client();
$res = $client->get('https://api.github.com/user', ['auth' => ['user', 'pass']]);
echo $res->getStatusCode(); // 200
echo $res->getBody(); // { "type": "User", ....
#2
63
We can use package Guzzle in Laravel, it is a PHP HTTP client to send HTTP requests.
我们可以使用Laravel的包Guzzle,它是一个PHP HTTP客户端来发送HTTP请求。
You can install Guzzle through composer
您可以通过composer安装Guzzle
composer require guzzlehttp/guzzle:~6.0
Or you can specify Guzzle as a dependency in your project's existing composer.json
或者,您可以在项目的现有组合.json中指定Guzzle作为依赖项
{
"require": {
"guzzlehttp/guzzle": "~6.0"
}
}
Example code in laravel 5 using Guzzle as shown below,
laravel 5中使用Guzzle的示例代码如下所示,
use GuzzleHttp\Client;
class yourController extends Controller {
public function saveApiData()
{
$client = new Client();
$res = $client->request('POST', 'https://url_to_the_api', [
'form_params' => [
'client_id' => 'test_id',
'secret' => 'test_secret',
]
]);
echo $res->getStatusCode();
// "200"
echo $res->getHeader('content-type');
// 'application/json; charset=utf8'
echo $res->getBody();
// {"type":"User"...'
}
#3
33
You just want to call an external URL and use the results? PHP does this out of the box, if we're talking about a simple GET request to something serving JSON:
您只想调用外部URL并使用结果?PHP是开箱即用的,如果我们讨论的是对服务JSON的对象的简单GET请求:
$json = json_decode(file_get_contents('http://host.com/api/stuff/1'), true);
If you want to do a post request, it's a little harder but there's loads of examples how to do this with curl.
如果你想做一个post请求,这有点难,但是有很多关于如何使用curl的例子。
So I guess the question is; what exactly do you want?
我想问题是;你到底想要什么?
#4
12
If you just need to fire the request to external API, you can use Laravel Request class as follows(Assuming you use GET request)
如果您只需要向外部API发出请求,您可以使用Laravel请求类,如下所示(假设您使用的是GET请求)
$request = Request::create('http://your-api.com', 'GET');
But if you need to get the content of the response, You can use Laracurl package as follows:
但是如果您需要获取响应的内容,可以使用Laracurl包,如下所示:
$response = Laracurl::get('http://your-api.com');
#5
2
You can make requests using Laravel and without external packages
您可以使用Laravel和没有外部包进行请求。
$request = \Illuminate\Http\Request::create('http://your-api.com', 'POST', ['param1' => 'value1', 'param2' => 'value2']);
Request functionality is provided because Laravel relies on Symfony's Request package.
之所以提供请求功能,是因为Laravel依赖于Symfony的请求包。
#6
1
You can use Httpful :
您可以使用Httpful:
Website : http://phphttpclient.com/
网站:http://phphttpclient.com/
Github : https://github.com/nategood/httpful
Github:https://github.com/nategood/httpful