PHP致命错误:未捕获异常“异常”与消息 [英] PHP fatal error: Uncaught exception 'Exception' with message
问题描述
致命错误:未捕获的异常异常与消息'
'的问题http://apitestserver.co.uk:9000/Service.svc/Items'在
/ var / www / html / e / connect_test.php:17堆栈跟踪:#0
/var/www/html/e/connect_test.php(39):
do_http_request('http:// apitest .. ..','hello')#1 {main}投入
/var/www/html/e/connect_test.php第17行
如果我发送到一个PHP脚本,只是抓住IP,然后它的工作,但如果我发送到API,它不。我的PHP脚本创建XML,然后转发到服务器。我收到错误,所以我刚刚创建了以下较小的脚本来纯粹测试连接:
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);
如果(!$ fp){
抛出新的异常($ url的问题);
}
$ response = @stream_get_contents($ fp);
if($ response === false){
throw new Exception(从$ 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;
如果我将URL更改为另一个服务器上的简单脚本,只需抓取IP传入连接并返回:
$ ip = $ _ SERVER ['REMOTE_ADDR'];
echo'IP equals ='。 $ IP;
然后它工作正常,没有错误。
更新 -
其上的错误会引发以下警告,可能是因为脚本未向API发送正确的信息
警告:fopen(http://apitestserver.co.uk:9000/Service.svc/Items)
[function.fopen]:无法打开流:HTTP请求失败! HTTP / 1.1
500根级别的数据无效。第1行,第1位。
另请注意,我可以使用提示人手动访问api服务器创建项目,它只是当这个脚本尝试连接和发送数据是一个问题。我写了另一个快速脚本,它连接并打印出默认的响应(提交的项目的rss feed)
当我运行'main'连接器脚本时,会抛出以下两个错误
警告:fopen()[function.fopen]:php_network_getaddresses:
getaddrinfo failed:名称或服务未知在
/var/www/html/e/sequence.php第65行
警告:fopen(http://apitestserver.co.uk:9000/Service.svc/Items)
[function.fopen]:无法打开流:操作现在正在进行
在/var/www/html/e/sequence.php第65行
我建议使用 curl ,而不是 fopen()
。卷曲更灵活,更安全。许多服务器禁用 allow_url_fopen ,卷曲当远程服务器上有问题时,也会更加优雅地失败。
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:
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. My PHP script creates XML and then forwards to the server. 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:
$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
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. I wrote another quick script which connects and prints out the default response (an rss feed of submitted items)
When I run the 'main' connector script it throws the following two errors
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 would suggest using curl instead of 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.
这篇关于PHP致命错误:未捕获异常“异常”与消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!