duansaoguan7955 2010-01-06 15:27
浏览 162
已采纳

使用php单击提交按钮

I have a webpage with a submit button and I would like php to parse the webpage and click the submit button and get the response (it can be a link or another html page.)

Is there any way to click a submit button using php?

I know there is something like htmlunit for java that allows one to pro-grammatically fill the form fields and click submit button. But I would like to do the same in php.

Thanks

  • 写回答

4条回答 默认 最新

  • duanbiyi7319 2010-01-06 15:53
    关注

    CURL will let you get the results of a form submission

    eg

    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, $urlOfFormSubmission);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    
            "field1"=>"data1",
            "field2"=>"data2"
    
        ));
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $contents = curl_exec($ch);
    

    You can also do the same thing with the PHP Stream functions

    eg

    $params = array('http' => array(
              'method' => "post",
              'content' => array("field1"=>"data1", "field2"=>"data2")
           ));
    
    $ctx = stream_context_create($params);
    
    $fp = @fopen($urlOfFormSubmission, 'rb', false, $ctx);
    
    if (!$fp)
    {
        throw new Error("Problem with ".$urlOfFormSubmission);
    }
    
    $contents = @stream_get_contents($fp);
    
    if ($contents === false)
    {
        throw new Error("Problem reading data from ".$urlOfFormSubmission);
    }
    

    In either case, $contents should contain the results of the form submission

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程