使用PHP CURL发布表单数据

时间:2014-03-16 13:46:13

标签: php curl

我一直在尝试使用PHP curl提交下面的表单,但它让我无法访问,当我只用下面的代码打开HTML文件并按提交时,它可以正常工作!

   <form action="http://www.example.com/47595ADEDFGVB558S" method="post">
          <input type="submit" name="warning" value="Yes, let me in">
    </form>

这是我的PHP块

    $url = 'http://www.example.com/47595ADEDFGVB558S';
    $data = array('warning' => 'Yes, let me in' );

$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt ($ch, CURLOPT_POST, 1);
    curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($data);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    $returndata = curl_exec($ch);
    curl_close($ch);
    echo $returndata ;

通过浏览器提交的标题

Request Method:POST
Status Code:302 Moved Temporarily
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,fr;q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:25
Content-Type:application/x-www-form-urlencoded
Host:www.example.com
Origin:http://www.example.com
Referer:http://www.example.com/47595ADEDFGVB558S
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Form Dataview sourceview URL encoded
warning:Yes, let me in
我在哪里做错了?我尝试设置origin,主机头但我认为错误与提交的数据有关。

0 个答案:

没有答案