简体   繁体   English

PHP致命错误:带有消息的未捕获异常“异常”

[英]PHP fatal error: Uncaught exception 'Exception' with message

I have a PHP script that connects to an api and posts information to their systems, but when its trying to connect it throws a fatal error: 我有一个PHP脚本连接到api并将信息发布到他们的系统,但是当它尝试连接它时会引发一个致命的错误:

Fatal error: Uncaught exception 'Exception' with message 'Problem with
'http://apitestserver.co.uk:9000/Service.svc/Items' in
/var/www/html/e/connect_test.php:17 Stack trace: #0 
/var/www/html/e/connect_test.php(39): 
do_http_request('http://apitest....', 'hello') #1 {main} thrown in 
/var/www/html/e/connect_test.php on line 17

If I send it to a PHP script which just grabs the IP then it works, but if I send it to the API it doesn't. 如果我将它发送到只捕获IP的PHP脚本,那么它可以正常工作,但是如果我将它发送到API它就不会。 My PHP script creates XML and then forwards to the server. 我的PHP脚本创建XML然后转发到服务器。 I was getting errors so I just created the following smaller script purely to test the connection: 我收到错误所以我只是为了测试连接而创建了以下较小的脚本:

function do_http_request($url, $data, $method = 'POST', 
    $optional_headers = 'Content-Type: application/atom+xml') {

  $params = array(
    'http' => array(
        'method' => $method,
        'content' => $data
    )
  );

  if ($optional_headers !== null) {
    $params['http']['header'] = $optional_headers;
  }

  $ctx = stream_context_create($params);
  $fp = fopen($url, 'rb', false, $ctx);
  if (!$fp) {
    throw new Exception("Problem with $url");
  }

  $response = @stream_get_contents($fp);
  if ($response === false) {
    throw new Exception("Problem reading data from $url");
  }

  $metaData = stream_get_meta_data($fp);
  fclose($fp);

  if(!preg_match('~^HTTP.*([0-9]{3})~', 
    $metaData['wrapper_data'][0], $matches)){
    throw new Exception('MALFORED RESPONSE - COULD NOT UNDERSTAND HTTP CODE');
  }
  if (substr($matches[1], 0, 1) != '2') {
    throw new Exception('SERVER REPORTED HTTP ERROR ' . $matches[1]);
  }
  return $response;
}
$data = 'hello';
$paul = 
  do_http_request('http://apitestserver.co.uk:9000/Service.svc/Items',$data);

echo $paul;

If I change the URL to a simple script on another one of our servers which just grabs the IP of the incoming connection and returns it: 如果我将URL更改为另一个服务器上的简单脚本,它只是获取传入连接的IP并返回它:

 $ip=$_SERVER['REMOTE_ADDR'];
 echo 'IP equals = ' . $ip;

Then it works fine with no errors. 然后它工作正常,没有错误。

Update - 更新 -

with errors on it throws the following warning, probably because the script is not sending the correct info to the API 有错误会抛出以下警告,可能是因为脚本没有向API发送正确的信息

Warning: fopen(http://apitestserver.co.uk:9000/Service.svc/Items) 
[function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 
500 Data at the root level is invalid. Line 1, position 1.

Also note that I can access the api server fine using fiddler to send manually created items across, its just when this script tries to connect and send data there is an issue. 另请注意,我可以使用fiddler来访问api服务器以手动创建项目,只是当此脚本尝试连接并发送数据时才会出现问题。 I wrote another quick script which connects and prints out the default response (an rss feed of submitted items) 我写了另一个快速脚本,连接并打印出默认响应(提交项目的RSS提要)

When I run the 'main' connector script it throws the following two errors 当我运行'main'连接器脚本时,它会抛出以下两个错误

Warning: fopen() [function.fopen]: php_network_getaddresses: 
getaddrinfo failed: Name or service not known in 
/var/www/html/e/sequence.php on line 65

Warning: fopen(http://apitestserver.co.uk:9000/Service.svc/Items) 
[function.fopen]: failed to open stream: Operation now in progress 
in /var/www/html/e/sequence.php on line 65

I just tested the URL in my browser and got a connection error. 我刚刚在浏览器中测试了URL并出现连接错误。

Maybe the problem is with their server (or you have the wrong URL)? 也许问题出在他们的服务器上(或者你的网址错误)?

Edit: 编辑:

Looks like ther server is throwing a 500 error - maybe because the data you're posting is invalid. 看起来像服务器正在抛出500错误 - 可能是因为您发布的数据无效。

You could use a packet sniffer and check the exact data you're sending to the server (or use cURL as suggested by @acrosman) 您可以使用数据包嗅探器并检查您发送到服务器的确切数据(或使用@acrosman建议的cURL)

apitestserver.co.uk doesn't respond to ping and the url is inaccessable. apitestserver.co.uk不响应ping并且url无法访问。 The API-server doesn't seem to be up. API服务器似乎没有启动。

I would suggest using curl instead of fopen() . 我建议使用curl而不是fopen() curl is both more flexible, and more secure. 卷曲更灵活,更安全。 Many servers disable allow_url_fopen , curl also fails more gracefully when there are problems on the remote server. 许多服务器禁用allow_url_fopen ,当远程服务器出现问题时,curl也会更优雅地失败。

First error message suggests problems with apitestserver - it returns error code 500, which states for "Internal server error". 第一条错误消息表明apitestserver存在问题 - 它返回错误代码500,其中指出“内部服务器错误”。

Second one informs that PHP can't resolve host name into IP address - I'd look into DNS configuration and/or hosts file. 第二个通知PHP无法将主机名解析为IP地址 - 我会查看DNS配置和/或主机文件。


Apparently 'fopen' function does not return proper stream. 显然'fopen'功能不会返回正确的流。 There can be many possibilities when that happens, but according to PHP manual, fopen function on fail should present E_WARNING message with proper commentary. 发生这种情况时可能有很多种可能性,但根据PHP手册,fopen函数on fail应该在E_WARNING消息中显示正确的注释。 It is possible that you have errors of that class silenced - call error_reporting(E_ALL) at the beginning of your script and look for some relevant error messages. 您可能会将该类的错误静音 - 在脚本开头调用error_reporting(E_ALL)并查找一些相关的错误消息。

或者您可以更改标题:

$optional_headers = 'Content-Type: application/json')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 致命错误:消息为“ 1048”的未捕获异常“ Exception” - Fatal error: Uncaught exception 'Exception' with message '1048 致命错误:消息为“ DateTimeZone ::?”的未捕获异常“ Exception” - Fatal error: Uncaught exception 'Exception' with message 'DateTimeZone::? 致命错误:消息'DateTime ::'未捕获异常'Exception' - Fatal error: Uncaught exception 'Exception' with message 'DateTime:: PHPExcel-致命错误:带有消息的未捕获异常“ Exception” - PHPExcel - Fatal error: Uncaught exception 'Exception' with message 致命错误:消息为“序列化的”的未捕获异常“ Exception” - Fatal error: Uncaught exception 'Exception' with message 'Serialization of PHP致命错误:未捕获的异常'Exception' - PHP Fatal error: Uncaught exception 'Exception' Phalcon PHP致命错误:消息“未知表达式”未捕获的异常 - Phalcon PHP Fatal Error: Uncaught exception with message 'Unknown Expression' PHP致命错误:消息为'SQLSTATE [42000]的未捕获异常'PDOException': - PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: PHP致命错误:消息为'SQLSTATE [42000]的未捕获异常'PDOException' - PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000] 消息“ FPDF错误”的未捕获异常“异常” - Uncaught exception 'Exception' with message 'FPDF error PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM