简体   繁体   English

如何在PHP中使用curl调用API

[英]How to call API using curl in php

I have developed some API using laravel and created a virtual host on my local machine. 我使用laravel开发了一些API,并在本地计算机上创建了虚拟主机。 like abc.xyz.com/api/v1/oauth2/token for getting oauth token. 例如abc.xyz.com/api/v1/oauth2/token,以获取oauth令牌。 Now I want to test these API 现在我要测试这些API
from another local app say abc.com using curl. 从另一个本地应用程序说使用curl的abc.com。

I am not getting any response .How can I do this ?Where both apps are in my local machine. 我没有任何回应。该怎么办?这两个应用程序都在我的本地计算机中。

Curl will give you a lot of trouble to do many things, use Guzzle to consume your API. Curl在做很多事情时会给您带来很多麻烦,请使用Guzzle消耗您的API。 It's simple, it has it all figured out and it works: 这很简单,已经弄清楚了,可以正常工作:

Install 安装

composer require "guzzlehttp/guzzle" "~4.0"

Create a client: 创建一个客户端:

$client = new \GuzzleHttp\Client();

Get results: 获得结果:

$response = $client->get('http://abc.example.com/api/v1/oauth2/token');

dd($response->getBody());

Project: https://github.com/guzzle/guzzle 项目: https//github.com/guzzle/guzzle

I got the answer for my question myself. 我自己回答了我的问题。 Actually I forgot to add / at the end of URL that why is was not giving any response . 实际上,我忘了在URL末尾添加/,为什么不给出任何响应。

Thanks to All for Help 感谢所有人的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM