Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I submit POST data using PHP and cURL?

Tags:

php

curl

Can anyone help on how can I do to send the data from a form (POST) to an URL using PHP and cURL?

Using cURL, all I want to accomplish is send the data, I don't want to be redirected nor get any sort of output (HTML nor TEXT) just submit the data.

It would be nice to know if the data has been submitted successfully or not to handle error or redirection.

Additional Info. The reason I think I'm getting redirected is because once I execute the cURL, the destination page, which is a third party website, have a redirect into place confirming the user that their data has been received, and for some reason, when I send my data using cURL their redirection it effects my page, therefore my page it also redirects to their confirmation site.

Thank you all.

Sample Code:

$sub_req_url ="http://domain.com/util/request";
$ch = curl_init($sub_req_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS,  "id=64&session=1&k=B0EA8835E&firstname=Name&lastname=Lastname&company=Company%20Name&[email protected]&work_phone=123-456-7890");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);

$resp = curl_exec($ch);
curl_close($ch);

I edit this post to show an example of what I'm using. I should posted the code in first place.

like image 630
Ole Media Avatar asked Aug 06 '11 18:08

Ole Media


People also ask

How do I send a POST request using PHP?

php $url = "https://reqbin.com/echo/post/form"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $headers = array( "Content-Type: application/x-www-form-urlencoded", ); curl_setopt($curl, CURLOPT_HTTPHEADER, $ ...

How do you Curl post payload?

To post JSON data using Curl, you need to set the Content-Type of your request to application/json and pass the JSON data with the -d command line parameter. The JSON content type is set using the -H "Content-Type: application/json" command line parameter. JSON data is passed as a string.

Does Curl use post?

Users can send data with the POST request using the -d flag. The following POST request sends a user and a pass field along with their corresponding values. POSTing with curl's -d option will include a default header that looks like: Content-Type: application/x-www-form-urlencoded .


1 Answers

function post($requestJson) {

    $postUrl = $this->endpoint."?access_token=".$this->token;

    //Get length of post
    $postlength = strlen($requestJson);

    //open connection
    $ch = curl_init();

    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL,$postUrl);
    curl_setopt($ch,CURLOPT_POST,$postlength);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$requestJson);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);

    //close connection
    curl_close($ch);

    return $response;
}
like image 50
horte Avatar answered Sep 24 '22 03:09

horte