使用cURL和PHP获得发布结果

时间:2014-08-01 12:22:55

标签: php curl html-form

我有一个执行表单提交的小功能,它应该返回结果(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));

1 个答案:

答案 0 :(得分:0)

您可以尝试一些不同的方法。 A simple example:

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