如何在外部网站上自动填写表格,并通过curl和php获得结果


how i fill automatically a form on external site and get results by curl and php

提交表单时,我如何在外部网站te.eg上通过区号电话号码自动填写表单重定向到另一个链接,如https://billing.te.eg/Arabic/BillStatus.aspx?Acc=A4002281809我希望从该页面的html中提取span元素之间的某些指定内容),如<span id="SpanPhoneNumber" dir="ltr">02-26981106</span><span id="SpanCurrentBalance">19.30</span>。。

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

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

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_FOLLOWLOCATION设置为true:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

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

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

如果存在卷曲错误代码,则获取卷曲错误代码:

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