使用cURL和PHP获取post结果


Get post result with cURL and PHP

我有一个执行表单提交的小函数,它应该返回结果(html)。

表单只有一个文本输入:

<input type="text" value="" maxlength="5" name="CODE" size="10">

我尝试用cURL填充这个变量:

//open connection
$ch = curl_init();
$myValue = "Test123";
$formUrl = "http://www.mywebsite.com/post.php";
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $formUrl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "CODE=".$myValue);
//execute post
$result = curl_exec($ch);
$info = curl_getinfo($ch);
//close connection
curl_close($ch);
echo $result;
我的问题是我没有得到任何结果…这个代码有什么问题吗?

非常感谢你的帮助!

编辑:

我也尝试过这种设置,结果是一样的:

$formData = array('CHKCODE' => $tesseractOutput);
    curl_setopt($ch, CURLOPT_URL, $formUrl);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
    curl_setopt($ch, CURLOPT_TIMEOUT, 120);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($formData));

您可以尝试不同的方法。一个简单的例子:

    $data = array("CODE" => "Test123");
    //$ch = curl_init("http://www.mywebsite.com/post.php");
    //EDIT
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    //EDIT
    curl_setopt($ch, CURLOPT_URL, "http://www.mywebsite.com/post.php");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
    $response = curl_exec($ch);
    var_dump($response);
    curl_close($ch);