Laravel 4 使用数据从控制器向外部 url 发出 post 请求

2022-01-08 00:00:00 http-post curl php laravel laravel-4

我正在寻找一种从控制器向外部 url 发出 post 请求的方法.发布的数据是一个 php 数组.要接收的 url 是外部 url 中的电子商务 API.帖子必须从控制器方法完成.url 应以success"、error"、failure"或trylater"字符串回复.我尝试了以下方法但没有成功:

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:

    return Redirect::to("https://backoffice.host.iveri.com/Lite/Transactions/New/Authorise.aspx", compact($array));

我也试过 curl:

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

正在发送的数组的一部分是 API 用来响应的回调:

Part of the array being sent is the callbacks that the API uses to responds:

'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'

请让我知道如何正确执行 POST 请求,并将其携带的数据发送到外部 url.来自控制器的 ajax 帖子也会有所帮助,但我尝试过但没有成功.但我更喜欢 laravel php 答案.谢谢.

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.

推荐答案

让我澄清一些事情并尝试为您指明正确的方向.

Let me clarify some stuff and try to point you in the right direction.

首先,您尝试执行的操作听起来像是从您的网络应用发出 API 请求".我的表述方式与您的表述方式的不同之处在于它更笼统.

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.

  1. 您可以在应用程序的任何位置发出 API 请求,而不必在控制器中(不要害怕为 API 调用之类的事情创建额外的类/模型!)
  2. 我很好奇为什么它必须"在你的控制器中完成?您的用例是什么?
  3. AJAX 不存在于服务器端(在 PHP 中).这纯粹是一种特定于 javascript 的技术",它描述了 javascript 在客户端向 URL 发出请求.

最后,你想做什么?您需要重定向用户吗?或者您是否需要在应用程序中进行 API 调用并解析结果?

您尝试的 cURL 请求应该用于发出 API 请求.这是在 PHP 代码中发出 API 请求的主要方式之一.但是,它不允许前端的用户看到正在发出和处理的请求.使用 cURL(和任何 API 请求),处理都在 PHP 的幕后进行(您的用户看不到).

The cURL request you've attempted should work 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).

相关文章