Laravel 4使用数据从控制器发送请求到外部网址 [英] Laravel 4 make post request from controller to external url with data

查看:350
本文介绍了Laravel 4使用数据从控制器发送请求到外部网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法,从控制器发送请求到外部网址。发布的数据是一个php数组。要接收的网址是外部网址中的电子商务API。该职位必须通过控制器方法完成。网址应该回复成功,错误,失败或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).

这篇关于Laravel 4使用数据从控制器发送请求到外部网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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