php Laravel 4 使用数据从控制器向外部 url 发出 post 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18770184/
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
Laravel 4 make post request from controller to external url with data
提问by Mwirabua Tim
I am looking for a way to make a post request from a controller to an external url. The data being posted is a php array. The url to recieve is an ecommerce API in an external url. The post has to be done from the controller method. The url should reply with 'success', 'error', 'failure' or 'trylater' string. I have tried the following with no success:
我正在寻找一种从控制器向外部 url 发出 post 请求的方法。发布的数据是一个 php 数组。要接收的 url 是外部 url 中的电子商务 API。帖子必须从控制器方法完成。该 url 应回复“成功”、“错误”、“失败”或“尝试稍后”字符串。我尝试了以下方法但没有成功:
return Redirect::to("https://backoffice.host.iveri.com/Lite/Transactions/New/Authorise.aspx", compact($array));
I have tried curl too:
我也试过卷曲:
$url = 'https://backoffice.host.iveri.com/Lite/Transactions/New/Authorise.aspx';
//url-ify the data for the POST
$fields_string ='';
foreach($array as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'& ');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($array));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
Part of the array being sent is the callbacks that the API uses to responds:
正在发送的数组的一部分是 API 用于响应的回调:
'Lite_Website_Successful_url' => 'https://mydomain.com/order/'.$order_id,
'Lite_Website_Fail_url' => 'https://mydomain.com/checkout/fail',
'Lite_Website_TryLater_url' => 'https://mydomain.com/checkout/trylater',
'Lite_Website_Error_url' => 'https://mydomain.com/checkout/error'
Please let me know how to do a POST request properly with data carried with it to an external url. An ajax post from the controller too would help but I have tried with no success. But I would prefer a laravel php answer more. Thank you.
请让我知道如何正确执行 POST 请求,并将数据携带到外部 url。来自控制器的 ajax 帖子也会有所帮助,但我尝试过但没有成功。但我更喜欢 laravel php 答案。谢谢你。
采纳答案by fideloper
Let me clarify some stuff and try to point you in the right direction.
让我澄清一些事情,并尝试为您指明正确的方向。
First, what you're attempting to do sounds like "making an API request from your web app". The difference in that wording in how I stated it vs yours is that it's more general.
首先,您尝试做的事情听起来像是“从您的网络应用程序发出 API 请求”。我的表述方式与您的表述方式的不同之处在于它更笼统。
- You can make an API request anywhere in your application, not necessarily in your controller (Don't be afraid to make extra classes/models for things like API calls!)
- I'm curious about why it "has to be" done in your controller? What's your use case?
- AJAX doesn't exist on the server-side (in PHP). That's purely a javascript-specific "technology" that describes javascript making a request to a URL on the client-side.
- 你可以在你的应用程序的任何地方发出 API 请求,不一定在你的控制器中(不要害怕为 API 调用之类的事情创建额外的类/模型!)
- 我很好奇为什么“必须”在您的控制器中完成?你的用例是什么?
- AJAX 不存在于服务器端(在 PHP 中)。这纯粹是一种特定于 javascript 的“技术”,它描述了 javascript 向客户端的 URL 发出请求。
Lastly, what are you trying to do? Do you need a user to be redirected? Or do you need to make an API call and parse the result within your application?
最后,你想做什么?您需要重定向用户吗?或者您是否需要进行 API 调用并在您的应用程序中解析结果?
The cURL request you've attempted shouldwork for making an API request. That's one of the main ways of making an API request within PHP code. It won't, however, allow a user on the front-end to see that request being made and processed. With cURL (and any API request), the processing is all happening behind the scenes in your PHP (which your users can't see).
您尝试的 cURL 请求应该适用于发出 API 请求。这是在 PHP 代码中发出 API 请求的主要方式之一。但是,它不会允许前端用户看到正在发出和处理的请求。使用 cURL(和任何 API 请求),处理都发生在 PHP 的幕后(您的用户看不到)。
回答by Mohammed Safeer
We can use package Guzzle in Laravel, it is a PHP HTTP client to send HTTP requests.
我们可以在 Laravel 中使用 Guzzle 包,它是一个发送 HTTP 请求的 PHP 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
或者您可以在项目现有的 composer.json 中将 Guzzle 指定为依赖项
{
"require": {
"guzzlehttp/guzzle": "~6.0"
}
}
Example code of POST Request in laravel, using Guzzle is as shown below,
laravel中POST请求的示例代码,使用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',
]
]);
$result= $res->getBody();
dd($result);
}
}
回答by minorgod
Either use CURL the way you've been trying, or check this thread for a brief answer on doing it with the Guzzle http client. Guzzle seems to be the preferred client for use with Laravel...
要么按照您一直尝试的方式使用 CURL,要么查看此线程以获取有关使用 Guzzle http 客户端执行此操作的简要答案。Guzzle 似乎是与 Laravel 一起使用的首选客户端......