PHP中HTTP请求的错误处理程序

时间:2014-12-17 09:28:01

标签: c# php

美好的一天!

我正在调试的程序是一个PHP站点,它通过SMS向用户发送数据。对于PHP站点,它应该与服务器中安装的C#应用​​程序连接。但是,当C#应用程序未打开时,会显示以下错误:

  
    

无法打开流:无法建立连接,因为目标计算机主动拒绝它。

  

=============================================== ================================ 注意: 1)以上错误代码并不孤单。它还有来自以下PHP代码的代码:

    <?php
      ini_set('error_reporting', E_ALL);
      ini_set("display_errors", 1);
      require ('connections.php');

      header('content-type: application/json; charset=utf-8');
      $num = $_GET['num'];
      $msg = $_GET['msg'];
      $id=explode(';',$msg);
      $id[0];
      $TaskID=substr($id[0],11);
      $msg = urlencode(str_replace(";", "\r\n", $msg));
      $response = file_get_contents('http://[ip-add]:8080/sms?num='.$num.'&msg='.$msg);
      ?>

2)如果你想知道&#34; msg&#34;包含,这里是javascript代码:

function savejo(){
var captcha = makeid();
var e = document.getElementById('selectCompanyID');
var cid = e.options[e.selectedIndex].value;
var cname = e.options[e.selectedIndex].text;
var e = document.getElementById('selectSiteID');
var sid = e.options[e.selectedIndex].value;
var sname = e.options[e.selectedIndex].text;
var e1 = document.getElementById('select1');
var select1 = e1.options[e1.selectedIndex].value;
var e2 = document.getElementById('select2');
var select2 = e2.options[e2.selectedIndex].value;
var e3 = document.getElementById('select3');
var select3 = e3.options[e3.selectedIndex].value;
var e4 = document.getElementById('select4');
var select4 = e4.options[e4.selectedIndex].value;
var e5 = document.getElementById('selectTechEngr');
var select5 = e5.options[e5.selectedIndex].text;
var phone = e5.options[e5.selectedIndex].value;
dbsave('txt3.php?do=store&Name='+select5+
'&JONumber='+document.getElementById('JONumber').value+
'&SRFNumber='+document.getElementById('SRFNumber').value+
'&datepicker='+document.getElementById('datepicker').value+
'&Departure='+document.getElementById('Departure').value+
'&Arrival='+document.getElementById('Arrival').value+
'&select1='+select1+
'&select2='+select2+
'&select3='+select3+
'&select4='+select4+
'&captcha='+captcha+
'&Objectives='+document.getElementById('Objectives').value+
'&SID='+sid+
'&CID='+cid, '', function(taskID){

    //var month = document.getElementById('cal_date_month').value;
    //var year = document.getElementById('cal_date_year').value;
    //goCurrentMonth('search3.php?month='+month+'&year='+year+'', 'mainpanel');
    var msg = 'Service Ref. No.%3A '+taskID+
    ';J.O. No.%3A '+document.getElementById('JONumber').value+
    ';SRF No.%3A '+document.getElementById('SRFNumber').value+
    ';Client%3A '+cname+
    ';Site%3A '+sname+
    ';Date%3A '+document.getElementById('datepicker').value+
    ';Time%3A '+document.getElementById('Departure').value.split(':').join('%3A')+' to '+document.getElementById('Arrival').value.split(':').join('%3A')+
    ';Verification Code%3A '+captcha+
    ';Objectives%3A '+document.getElementById('Objectives').value;
    //togglepanel('addpanel', 0);
    if (taskID !== undefined) {
        sendsms(phone, msg);
        updatetable();
    }    
});

}

3)在上面的php代码中删除了http地址以保护隐私。

=============================================== ============================

现在,我的问题是:

1)我如何处理这样的错误,我可以输入以下php代码:

[code from above php]
echo $response; 
if($response== <-- [what logical phrase can i use to check if the response is error?])
{
    $Status='Message not sent since C# application is not open';
    $query="Update dbo.Table4 set Status='$Status' where TaskID='$TaskID'";
    $objQuery = sqlsrv_query($dbhandle, $query); 
     //Close connection
}
else
{
   echo json_encode( $response);
}

2)上面的消息是由哪个应用程序创建的?

1 个答案:

答案 0 :(得分:0)

  1. 使用curl而不是file_get_contents。然后,您可以获取http状态代码并在错误上获得错误结果。 http://php.net/manual/en/book.curl.php
  2. 我不明白这个问题,file_get_contents无法连接到c#app并抛出错误。
  3. 我希望它有所帮助