1

I'm using Webex's URL API and for some reason, when writing a simple PHP cURL request to the URL, the API returns failure. But if I pass in the same post parameters into a form, and the form's action attribute equals that API endpoint, the API returns success.

Here's the form method:

  <form action="xxxxxxx987" name="hidden_form" method="post">
      <input value="EN" name="AT" type="hidden" />
      <input value="xxxxxxx987" name="MK" type="hidden" />
      <input value="<?php echo $email; ?>" name="AE" type="hidden" />
      <input value="<?php echo $firstname; ?>" name="FN" type="hidden" />
      <input value="<?php echo $lastname; ?>" name="LN" type="hidden" />
      <input value="<?php echo $company; ?>" name="CO" type="hidden" />
      <input value="http://mysite.com/resources/thank_you" name="BU" type="hidden" />
  </form>

And here's the cURL method:

$url = "https://mysite.com/m.php";
//Data Array
$postParams = array("AT"=>"EN",
                    "MK"=>"xxxxxxx987",
                    "AE"=>"my@email.com",
                    "FN"=>"fname",
                    "LN"=>"lname",
                    "CO"=>"my company",
                    "BU"=>"http://192.168.x.x/resources/thank_you");
//Encode Query Data
$data = http_build_query($postParams);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true); //True For Regular HTTP Post
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded", "Content-length: ".strlen($data)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);

if($result) {
    echo '<h3>Status: Curl Succeeded</h3>';
    print 'Result: '.$result;
}

The question: Why does the API fail when I curl it and why does it succeed when using it as the form post action? What's wrong with the cURL method?

Kristian
  • 21,204
  • 19
  • 101
  • 176
  • Have you got [Wireshark](http://www.wireshark.org/download.html) handy? I would look at both requests and see what the difference is... – DaveRandom Aug 31 '11 at 19:25
  • 2
    don't need specific `curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded", "Content-length: ".strlen($data))); ` if you set `CURLOPT_POST` as `true` the cURL write the headers above in headers to send. – Kakashi Aug 31 '11 at 19:31
  • anyway there will be an responde in curl_exec. what you should be do is check the http codes and parsing the html returned – The Mask Aug 31 '11 at 19:42

1 Answers1

1

You don't need http_build_query because CURLOPT_POSTFIELDS accepts an array

Brian Glaz
  • 15,468
  • 4
  • 37
  • 55