【问题标题】:how i fill automatically a form on external site and get results by curl and php我如何在外部网站上自动填写表格并通过 curl 和 php 获得结果
【发布时间】:2015-06-12 06:40:33
【问题描述】:

当表单提交时,我如何通过区号电话号码在外部站点te.eg上自动填写表单 >redirect 到另一个链接,如 https://billing.te.eg/Arabic/BillStatus.aspx?Acc=A4002281809 我想 Extract 从此页面的 html (span 元素之间的一些指定内容),如 <span id="SpanPhoneNumber" dir="ltr">02-26981106</span> AND @987654324 @..

这是我想在外部网站上填写的表单代码

<td style="width:800px;vertical-align:top;">
                                 <div style="width: 95%; padding-right: 20px; padding-top: 35px; margin-right: 30px;font-size:14px;">
                                <table>
                                    <tbody><tr>
                                        <td>كود المنطقة
                                        </td>
                                        <td>رقم التليفون
                                        </td>
                                        <td style="white-space: nowrap">
                                            <div id="PinCodeLabel" style="overflow: hidden; width: 0px;">
                                                الكود السرى
                                            </div>
                                        </td>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <input id="TxtAreaCode" type="text" placeholder="كود المنطقة" style="width: 67px;" value="02" onkeyup="javascript:InquirySubmit(event);" maxlength="3" onkeydown="javascript:InquiryByTelephoneValueChanged();">
                                            <input type="hidden" id="TxtAreaCodeRegex" value="^[0-9]{1,3}$">
                                            <input type="hidden" id="TxtAreaCodeValidationEmpty" value="من فضلك ادخل كود المنطقة">
                                            <input type="hidden" id="TxtAreaCodeValidationInvalid" value="'كود المنطقة من رقم إلا ثلاثة أرقام، مثلا '02">
                                        </td>
                                        <td>
                                            <input id="TxtPhoneNumber" type="text" placeholder="رقم التليفون" style="width: 120px;" onkeyup="javascript:InquirySubmit(event);" maxlength="8" onkeydown="javascript:InquiryByTelephoneValueChanged();">
                                            <input type="hidden" id="TxtPhoneNumberRegex" value="^[0-9]{6,8}$">
                                            <input type="hidden" id="TxtPhoneNumberValidationEmpty" value="من فضلك ادخل رقم التليفون">
                                            <input type="hidden" id="TxtPhoneNumberValidationInvalid" value="يتكون رقم التليفون من 6-8 أرقام">
                                        </td>
                                        <td>
                                            <div id="PinCodeInput" style="overflow: hidden; width: 0px;">
                                                <input id="TxtPinCode" type="password" placeholder="الكود السرى" style="width: 73px;" onkeyup="javascript:InquirySubmit(event);" maxlength="4">
                                                <input type="hidden" id="TxtPinCodeRegex" value="^[0-9]{4,4}$">
                                                <input type="hidden" id="TxtPinCodeValidationEmpty" value="من فضلك ادخل الكود السرى">
                                                <input type="hidden" id="TxtPinCodeValidationInvalid" value="الكود السرى مكون من أربعة أرقام">
                                            </div>
                                        </td>
                                        <td style="vertical-align: top;">
                                            <div style="border: 2px solid; border-radius: 90px; height: 30px; background-color: #ee2226; box-shadow: 2px 2px 5px #888888; border-color: #ee2226; padding-top: 3px; cursor: pointer;" onclick="javascript:Inquiry();">
                                                <table>
                                                    <tbody><tr>
                                                        <td style="vertical-align: top; padding-right: 5px;">
                                                            <img alt="" src="Images/InvoiceIcon.png" style="border-width: 0px"></td>
                                                        <td style="vertical-align: middle; text-decoration: none; color: white; font-size: 14px; font-weight: bold; white-space: nowrap; padding-left: 5px;">اعرض الفواتير</td>
                                                        <td>
                                                            <div id="BtnShowBillsLoading" style="width: 0px; overflow: hidden;">
                                                                <img alt="" src="Images/loading0.gif">
                                                            </div>
                                                        </td>
                                                    </tr>
                                                </tbody></table>
                                            </div>
                                        </td>
                                    </tr>
                                </tbody></table>
                                 </div>
                        </td>

我尝试了这个功能,但它不起作用,不显示任何结果或错误

  function httpPost($url,$params)
    {
      $postData = '';
       //create name value pairs seperated by &
       foreach($params as $k => $v) 
       { 
          $postData .= $k . '='.$v.'&'; 
       }
       rtrim($postData, '&');

        $ch = curl_init();  

        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch,CURLOPT_HEADER, false); 
        curl_setopt($ch, CURLOPT_POST, count($postData));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);    

        $output=curl_exec($ch);

        curl_close($ch);
        return $output;
    }
    $params = array(
       "TxtAreaCode" => "02",
       "TxtPhoneNumber" => "26981106"
    );

    echo httpPost("https://billing.te.eg/Arabic/BillInquiry.aspx",$params);

【问题讨论】:

  • 您发布的表单代码并不是真正的表单。它调用 InquirySubmit 函数,该函数可能使用 ajax 将输入字段值发布到另一个页面。您必须找出它发布到的那个页面的 url,它可能与 BillInquiry.aspx 完全不同,并且该函数可以重命名变量。使用浏览器开发工具上的“网络”选项卡来捕获和反向工程 ajax 调用,然后您将有足够的信息来构建您的 curl。
  • 另外,我的表单使用随机数,所以我只接受第一次访问表单页面并启用 javascript 的人的提交。它可以防止机器人滥用我的表单,并且可以防止你将帖子卷入我的表单。如果网站是现代的,他们会阻止您尝试的内容
  • @RightClick thanx 兄弟为您的解释,是的,您是对的,它不是 html 表单,他们在此链接中使用 javascript 我认为 https://billing.te.eg/Arabic/Scripts/PhoneAreaCodeInquiry.js?A=1 但我如何将它与 curl 一起使用,这是我的问题,我需要一个例子
  • 查看页面时,按 F12。它将调出开发人员工具。顶部是一堆选项卡,元素通常是第一个。单击名为网络的那个,如果其中有任何内容,请使用清除按钮清除屏幕。当您坐在那里时,可能会在后台发生一些请求,请清除它们。然后,当您准备好时,提交表单。您会看到您的新请求出现在底部。单击它并转到“标题”选项卡。这将向您显示它发布到的 url 以及所有变量。应该是你需要的一切。
  • Request URL:https://billing.te.eg/English/BillInquiry.aspx/InquiryByPhone Request Method:POST 区号:"02" 电话号码:"26981106" 密码:""

标签: php curl


【解决方案1】:

curl_setopt($ch, CURLOPT_HEADER, true);

如果标头是您想要获取的唯一响应,请将其设置为 true:

curl_setopt($ch, CURLINFO_HEADER_OUT, true);

如果没有问题,则从 HTML 中删除标题:

$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
$data= substr($data,$skip);

可能会有重定向所以设置CURLOPT_FOLLOWLOCATIONtrue:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

要获得有关问题的更多信息,您需要更多选项:

curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_TIMEOUT,100);
curl_setopt($ch, CURLOPT_HEADER, true);

如果有则获取 curl 错误码:

  $data = curl_exec($ch);
  if (curl_errno($ch)){
      $data .= 'Retreive Base Page Error: ' . curl_error($ch);
      echo $data;
  }

否则获取其余信息:curl_getinfo()

  else {
    $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
    $responseHeader = substr($data,0,$skip);
    $data= substr($data,$skip);
    $info = curl_getinfo($ch);
    $info = var_export($info,true);
   }
   echo $responseHeader . $info . $data;

【讨论】:

  • 谢谢兄弟,但我找到了正确的代码,我必须通过json 将数据发布到表单中,但我现在的问题是如何提取 HTML 内容
猜你喜欢
  • 2012-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-06
  • 2011-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多