cURL帮助PHP

时间:2009-11-02 02:56:33

标签: php curl

我正在玩cURL,我无法让它工作。我希望cURL脚本在该表单上发布数据,以便我可以在results.txt中看到发布的数据。这是我的表单脚本,然后是我的cURL脚本。我在发布之前更改了$ url。

编辑:问题是,它没有发布数据。我运行了cURL脚本,并检查了results.txt以查找发布数据“WORKS”并且它不存在。此外,如果它有任何区别,我正在使用DreamHost。

更新:我知道了!有趣的事情。我的目标是使用表单而不是处理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代码源码     

    $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);


?>

我的cURL脚本代码来源:

<?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;

?>

1 个答案:

答案 0 :(得分:3)

您正在使用CURLOPT_POSTFIELDS传递字符串。请尝试使用数组。例如: array('anything'=&gt;'WORKS');