Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I forward $_POST with PHP and cURL?

Tags:

post

php

curl

api

I receive a POST request at my PHP script and would like to forward this POST call to another script using POST too. How can I do this?

I can use cURL if it's required for this action.

like image 243
Tom Smykowski Avatar asked Apr 27 '10 22:04

Tom Smykowski


People also ask

Can I use cURL in PHP?

PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols.

How do I forward a request in PHP?

Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php . You can also specify relative URLs.

How does PHP handle cURL request?

php file with the following contents. $url = 'https://www.example.com' ; $curl = curl_init(); curl_setopt( $curl , CURLOPT_URL, $url );

How let cURL use same cookie as browser in PHP?

The only way this would work is if you use persistent cookies in your curl request. CURL can keep cookies itself. Assign a session ID to the cookie file (in curl) so subsequent requests get the same cookies. When a user clicks a link, you will need to curl the request again.


3 Answers

Do this,

curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($_POST));
like image 149
ZZ Coder Avatar answered Oct 10 '22 12:10

ZZ Coder


Here's a fully functional cURL request that re-routes $_POST where you want (based on ZZ coder's reply)

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://urlOfFileWherePostIsSubmitted.com");
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    // ZZ coder's part
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
    $response = curl_exec($ch);
    curl_close($ch);
like image 45
Robert Sinclair Avatar answered Oct 10 '22 13:10

Robert Sinclair


Perhaps:

curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);

From curl_setopt:

This can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value.
like image 13
Natalie Adams Avatar answered Oct 10 '22 11:10

Natalie Adams