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

时间:2015-04-07 15:36:15

标签: php curl

当表单提交时,如何通过区号电话号码自动填写外部网站te.eg上的表单 重定向到另一个链接,例如https://billing.te.eg/Arabic/BillStatus.aspx?Acc=A4002281809我希望提取来自此页面的html( span 元素之间的某些指定内容),例如{{ 1}}和<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>

1 个答案:

答案 0 :(得分:0)

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为真:

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;