我想要的是从API获取一个带有HTTP(例如,jQuery的AJAX)请求的对象到外部api.我该如何开始?我对谷歌先生进行了研究,但我找不到任何帮助.
我开始怀疑这是否可能?在这篇文章中,Laravel 4从控制器向外部URL发出请求数据,看起来它可以完成.但是没有任何示例或任何来源可以找到一些文档.
请帮帮我?
我正在寻找一种方法来从控制器发出一个发布请求到外部网址.发布的数据是一个php数组.要接收的网址是外部网址中的电子商务API.帖子必须从控制器方法完成.该网址应回复"成功","错误","失败"或"trylater"字符串.我试过以下但没有成功:
return Redirect::to("https://backoffice.host.iveri.com/Lite/Transactions/New/Authorise.aspx", compact($array));
Run Code Online (Sandbox Code Playgroud)
我也试过卷曲:
$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);
Run Code Online (Sandbox Code Playgroud)
要发送的数组的一部分是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'
Run Code Online (Sandbox Code Playgroud)
请告诉我如何使用随附的数据正确地发送POST请求到外部URL.来自控制器的ajax帖子也会有所帮助,但我尝试过没有成功.但我更喜欢laravel php的答案.谢谢.