Php – cURL help with PHP

curlPHP

I am playing with cURL, and I am having trouble getting it to work. I want the cURL script to post the data on that form so I can see the posted data in results.txt. Here is my form script and then my cURL script after. I changed the $url before posting here.

Edit: The problem is, it's not posting the data. I ran the cURL script, and checked the results.txt to find the post data "WORKS" and it's absent. Also, if it makes any difference, I am using DreamHost.

UPDATE: I GOT IT! Funny thing. I targeted to the page with the form instead of the page that processes the POST.

<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"/>
    <title>Test</title>
  </head>

  <body>
    <form method="POST" name="TestForm" action="write.php">
      <p />
      Input anything: <input type="text" name="anything" value="Default"/> <br />
      <input type="submit" value="OK" name="submit" />
    </form>
  </body>
</html>

write.php code source

    $stringData1 = $_POST["anything"];
    $myFile = "results.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = "Bobby Bopper\n";
    fwrite($fh, $stringData);
    fwrite($fh, $stringData1);
    fclose($fh);


?>

My cURL script code source:

<?php

$url = "http://www.domain.com/submit/index.php";


$useragent="YahooSeeker-Testing/v3.9 (compatible; Mozilla 4.0; MSIE 5.5; http://search.yahoo.com/)";

$ch = curl_init();


curl_setopt($ch, CURLOPT_USERAGENT, $useragent);


curl_setopt($ch, CURLOPT_URL, $url);


curl_setopt($ch, CURLOPT_POST, 1);


curl_setopt($ch, CURLOPT_POSTFIELDS, "anything=WORKS");


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


print "<br/> test" . $result;

?>

Best Answer

you are passing a string with CURLOPT_POSTFIELDS. Try using an array instead. Ex: array( 'anything' => 'WORKS' );