0

I have a simple form which i want to convert into a PHP backend system.

Now this form has an action to submit to a URL - the URL only accepts an invite only when a data with the name postdata and correct information is submitted (xml which has been encoded).

Works: - Note that the input name is called postdata and the value contains the xml which has been urlencoded and it works perfectly.

<form name="nonjava" method="post" action="https://url.com">
    <input name="postdata" id="nonjavapostdata" type="hidden" value="&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;" />
    <button type="submit" name="submit">Button</button>
</form>

However, i want to achieve the following by moving the postdata element within PHP as a lot of information is passed through that way.

Please note: the link is https but it wasnt working on local so i had to disable it within the CURL.

<?php
    $url = 'https://super.com';
    $xmlData = '<?xml version="1.0" encoding="utf-8"?>
    <postdata
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema">
         <api>2</api>
    </postdata>';

    $testing = urlencode($xmlData);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "postdata=" . $testing);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_VERBOSE, true);

    $response = curl_exec($ch);

    // Then, after your curl_exec call:
    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $header = substr($response, 0, $header_size);
    $body = substr($response, $header_size);

    echo $header_size . '<br>';
    echo htmlspecialchars($header) . '<br>';
    echo htmlspecialchars($body) . '<br><br>';

    $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    echo "$http_status for $url <br>";
?>

I keep getting a 302 error (which is the url content is not finding the data so its redirecting to a not found page.)

jagmitg
  • 4,236
  • 7
  • 25
  • 59

3 Answers3

0

302 is NOT error; it just means you need to go to the redirected URL. A browser does that automatically for you. In PHP code, you would have to do it yourself. Look at the Location header on 302.

I tried to look if there is a way that Curl could be told to follow through redirects; many language HTTP libraries support that. This thread seems to suggest that CuRL also supports it - Is there a way to follow redirects with command line cURL

Community
  • 1
  • 1
Amit
  • 1,836
  • 15
  • 24
0

Your code works as it is supposed to, I am getting POST data:

Array ( [postdata] => <?xml version="1.0" encoding="utf-8"?> <postdata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/20 01/XMLSchema"> <api>2</api> </postdata> )

as a result. The problem is on the receiving side, it isnt handling it correctly.

Angel Iliikov
  • 710
  • 5
  • 8
  • Pretty much - post data i.e. the xml data needs to be htmlencoded and send to the url. As said, the HTML version of the code works perfectly without any php. – jagmitg Oct 20 '15 at 12:30
  • Not really sure what you are trying to achieve? Perhaps you want to call htmlentites on it: `$testing = urlencode(htmlentities($xmlData));` ? – Angel Iliikov Oct 20 '15 at 13:09
0

You need to follow the 302 redirect in curl. This is relatively easy to do by adding the location flag. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); This is the equivalent of doing curl -L at the CLI. You can see an example here in the php documentation.

Jake Sylvestre
  • 936
  • 3
  • 14
  • 39