5

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=me@gmail.com&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.

hakre
  • 193,403
  • 52
  • 435
  • 836
Ole Media
  • 1,652
  • 9
  • 25
  • 36
  • 1
    Show the code that you have. Sounds like the problem is there. Your question has no concrete information to go by. (Also "can anyone help" is not much of a technical inquiry.) – mario Aug 06 '11 at 18:47
  • Well... wouldn't you want the site you're POSTing to to redirect? Most log-in pages will redirect you to an authenticated homepage when you've successfully logged in. That being said, if the redirect is changing how the page you're on looks, it sounds like you're not handling the cURL response properly. As mario said, code would be helpful – wolv2012 Aug 06 '11 at 18:52
  • 1
    possible duplicate of [Passing $_POST values with cURL](http://stackoverflow.com/questions/28395/passing-post-values-with-curl) – mario Aug 06 '11 at 18:52
  • @mario and wolv2012 - edited the post trying to make more sense. – Ole Media Aug 06 '11 at 19:00
  • If the POST request initiated via cURL experiences a HTTP redirect, then the cURL API will not magically transfuse that into the running PHP scripts response headers. Your description of the events does not match with the shown code. – mario Aug 07 '11 at 01:37
  • @mario Seems that once the third party website receives my data, it process it and redirects to a confirmation page, making my page to redirect as well. Is there a way to avoid this to happen? – Ole Media Aug 07 '11 at 02:54
  • No, that's not possible. What happens in curl stays in curl. Redirects on the POST request do not affect another servers php script. (Unless of course you dissimulate other relevant code parts.) – mario Aug 07 '11 at 06:46
  • @mario - What you are saying makes perfect sense, but I can prove you wrong and for some reason what happens to cURL it also effects to my page. If you don't mind, please email me at my nickname @ gmail.com and I can send you a link to the page. Unfortunately, due to privacy I cannot reveal more info to the public. – Ole Media Aug 07 '11 at 18:55

2 Answers2

7
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;
}
horte
  • 403
  • 4
  • 8
  • I'm still getting redirected, and I believe is due to the page that I'm sending the data. Seems that once that page, the third party website, receives my data, if data is approved it redirects to a confirmation page. Is there a way to just send the data and display my custom confirmation page? and not their page? – Ole Media Aug 06 '11 at 22:15
0

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

Please search the site and/or post your code how you do it. Looks like that doing the actual POST request is not your concrete problem. In case it is, take a look at this related question: Passing $_POST values with cURL and php curl: i need a simple post request and retrival of page example

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.

Assuming $ch is your curl handle:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);

The meaning of the options are described on the PHP manual page. Then do your curl_exec and check the return value if the request was successfull or not.

A probably related question for that part is: Make curl follow redirects?

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836