首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将自定义错误消息返回到PHP的cURL?

如何将自定义错误消息返回到PHP的cURL?
EN

Stack Overflow用户
提问于 2014-08-22 04:57:24
回答 2查看 802关注 0票数 0

我正在使用PHP cURL-multi在另一台服务器上发出HTTP请求。另一台服务器上的脚本在Perl/CGI中。在这一端出现致命错误的情况下,我想抛回一个500 Server错误(除非有更好的方法)。

这是我的PHP。注意,fb()是对FirePHP的记录器的过程性调用。

代码语言:javascript
复制
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://otherserver.com/cgi-bin/myCgiScript.cgi");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// grab URL and pass it to the browser
$result = curl_exec($ch);
if ($result === false) {
    $errno = curl_errno($ch);
    $error = curl_error($ch);
    print "errno:\n";
    var_dump($errno);
    print "error:\n";
    var_dump($error);
} else {
    print "result:\n";
    var_dump($result);
}
$info = curl_getinfo($ch);
print "info:\n";
var_dump($info);

curl_close($ch);

下面是来自另一台服务器的示例Perl CGI文件:

代码语言:javascript
复制
#!/usr/bin/perl

use strict;
use warnings;
use CGI;
use Try::Tiny;

my $q = new CGI();

try {

   # Code that might die().

   print $q->header({
      -type => "application/json",
      -status => "200 OK"
   });

   print $results;

} catch {
   my $message = $_;
   chomp($message);

   print $q->header({
      -type => "application/json",
      -status => "500 Server Error",
      -message => $message
   });

};

但是无论我做什么,我似乎都不能从cURL中获取“消息”文本。(我也不能得到"500服务器错误“中的”服务器错误“部分。我得到了一个事实,我得到了一个500错误,但没有相应的文本。

编辑:

我简化了PHP示例。

现在它不是在走“失败”的道路...也就是说,如果我使用CURLOPT_HEADER = 0,则返回"";如果使用CURLOPT_HEADER = 1,则返回500Server错误的文本

代码语言:javascript
复制
result:
string(398) "HTTP/1.1 500 Server Error
Date: Fri, 22 Aug 2014 15:32:38 GMT
Server: Apache/2.2.21 (Unix) DAV/2 mod_ssl/2.2.21 OpenSSL/1.0.0e
Message: malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "(end of string)") at /opt/apache/cgi-bin/CSU_SIGNUP_2.0/getCsuAttributes.cgi line 45
Content-Length: 0
Connection: close
Content-Type: application/json

"
info:
array(20) {
  ["url"]=>
  string(46) "http://otherserver.com/cgi-bin/myCgiScript.cgi"
  ["content_type"]=>
  string(16) "application/json"
  ["http_code"]=>
  int(500)
  ["header_size"]=>
  int(398)
  ["request_size"]=>
  int(113)
  ["filetime"]=>
  int(-1)
  ["ssl_verify_result"]=>
  int(0)
  ["redirect_count"]=>
  int(0)
  ["total_time"]=>
  float(1.117776)
  ["namelookup_time"]=>
  float(0.006001)
  ["connect_time"]=>
  float(0.008552)
  ["pretransfer_time"]=>
  float(0.266607)
  ["size_upload"]=>
  float(0)
  ["size_download"]=>
  float(0)
  ["speed_download"]=>
  float(0)
  ["speed_upload"]=>
  float(0)
  ["download_content_length"]=>
  float(0)
  ["upload_content_length"]=>
  float(0)
  ["starttransfer_time"]=>
  float(1.11773)
  ["redirect_time"]=>
  float(0)
}
EN

回答 2

Stack Overflow用户

发布于 2014-08-22 05:02:55

你能做像这样的重定向吗?

代码语言:javascript
复制
header( 'Location: http://www.yoursite.com/error.php?error=5000' ) ; 

然后让你的php使用$_GETerror';然后让php来做这些工作?

票数 0
EN

Stack Overflow用户

发布于 2014-08-22 23:49:33

您说过您的内容类型是JSON,但事实并非如此(“格式错误的JSON字符串”)。为错误设置文本的内容类型,就可以了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25435792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档