简体   繁体   English

如何使 API POST 调用请求的重定向与 HTML POST 请求完全相同

[英]How to make redirect for API POST call request works exactly same like HTML POST request

Below is my code using curl and it's not really redirect like HTML form :下面是我使用 curl 的代码,它不像 HTML 表单那样真正重定向:

$curl =  new \Curl\Curl();
        $curl->setHeader('Content-Type', 'application/x-www-form-urlencoded');
        $curl->setOpt(CURLOPT_FOLLOWLOCATION,true);
        $curl->setOpt(CURLOPT_POST,true);
        $curl->setOpt(CURLOPT_RETURNTRANSFER,true);
        $curl->post('http://localpay.sample.test/make-payment',$params);

Hi guys,嗨,大家好,
above are the sample code that I made using php package : curl/curl ,以上是我使用php 包制作的示例代码: curl/curl
but don't worry,I'm looking forward for any answer,answer using pure curl also can and any other answer also can as long it's working with php language.但别担心,我期待着任何答案,使用纯 curl 的答案也可以,任何其他答案也可以,只要它使用 php 语言。

What I'm trying to achieve is how to do an API call works exactly the same like HTML form.我想要实现的是如何进行 API 调用,其工作方式与 HTML 表单完全相同。 Why I want to achieve this ?为什么我要实现这一目标? because in the HTML form we need to click the submit button,I know javascript can do the trigger but I'm trying to build my own package with totally back-end language.因为在 HTML 表单中,我们需要单击提交按钮,我知道 javascript 可以触发,但我正在尝试使用完全后端语言构建自己的包。

I already tried search for the questions on the internet, but none was what I expected.我已经尝试在互联网上搜索问题,但没有一个是我所期望的。

And below is my simple html form and it does redirect to the desired url :下面是我的简单 html 表单,它确实重定向到所需的 url :

<form id="payment_confirmation" action="http://localpay.sample.test/make-payment" method="post"/>

   Payer ID : <input type="text" name="payer_acc_id"><br>
   Payee ID : <input type="text" name="payer_acc_id"><br>
   <input type="hidden" name="signature" value="somePhpFunction()">

    <button type="submit" value="Submit"> Submit</button>

</form>

Above is the sample working HTML form and it's redirect to that action url path but in curl I fail to achieve it, so how do I achieve this by using curl or any other ways also can.以上是示例工作 HTML 表单,它重定向到该操作 url 路径,但在 curl 中我无法实现它,所以我如何通过使用 curl 或任何其他方式来实现这一点也可以。

setting the Content-Type header does not dictate which format curl will use to send the post data.设置Content-Type标头并不规定 curl 将使用哪种格式发送帖子数据。 if you give CURLOPT_POSTFIELDS an array then curl will send the data using the multipart/form-data format, regardless of what you put in the Content-Type header.如果你给 CURLOPT_POSTFIELDS 一个数组,那么 curl 将使用multipart/form-data格式发送multipart/form-data ,不管你在 Content-Type 标头中放了什么。 most likely your curl code sends the data in the Multipart/form-data format and $params is an array, and because you tell the server this is application/x-www-form-urlencoded , the server ties to parse the multipart/form-data data as application/x-www-form-urlencoded , (which is a completely incompatible format), and doesn't understand the data at all.很可能您的 curl 代码以 Multipart/form-data 格式发送数据,而 $params 是一个数组,并且因为您告诉服务器this is application/x-www-form-urlencoded ,服务器会绑定到解析 multipart/form -data 数据为application/x-www-form-urlencoded ,(这是一种完全不兼容的格式),并且根本不理解数据。 if you want to convert an array of post data to the application/x-www-form-urlencoded -format, use the http_build_query function, eg如果要将发布数据数组转换为application/x-www-form-urlencoded -format,请使用http_build_query函数,例如

    $curl->post('http://localpay.sample.test/make-payment',http_build_query($params));

I found the answer for POST redirect using curl :我使用 curl 找到了 POST 重定向的答案:

$params = ['payee_id' => 1 , 'amount' => 300];

$curl = curl_init('http://someurl.com/silent/pay');
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));    
        curl_exec($curl);

curl will follow the redirect for POST method, if you guys want more explanation,go here . curl 将遵循 POST 方法的重定向,如果你们想要更多解释,请到这里

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

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