php guzzle OpenCPU POST请求在执行期间卡住一个简单的R包函数

问题描述 投票:0回答:3

php guzzle OpenCPU POST请求简单的R DemoPackage函数

$response = $client->request('POST','http://178.254.13.220/ocpu/library/DemoPackage/R/addition/json', ['json' => $jsontext, 'debug' => true]);

在ubuntu服务器上执行期间卡住了。简单的加法函数添加2个数字并返回总和作为结果(参见下面的代码)。

如果“POST”更改为“GET”,则请求通过以下命令在变量体中提供函数添加的正确源代码:$ body = $ response-> getBody();

在开发和测试功能的本地Mac计算机上,DemoPackage工作正常。我在ubuntu服务器上的其他R包运行完美,但是我在这个添加2个数字的简单例子中找不到错误。

 addition <- function(jsontext)
 {
  tmp <- unlist(jsonlite::fromJSON(jsontext))
  a  <- as.numeric(tmp[[1]])
  b  <- as.numeric(tmp[[2]])
  c <- a + b

  aValue <- as.character(round(a))
  bValue <- as.character(round(b))
  cValue <- as.character(round(c))
  pValue_dataframe <- data.frame(aValue,bValue,cValue)
  result           <- list(sum = pValue_dataframe)

  result_json <- toJSON(result)

  return(result_json)
 }

执行POST请求时,php代码很糟糕。使用http请求调试选项会出现错误消息,但是我无法解释它或知道该怎么做。错误消息是:

找到主机178.254.13.220的捆绑包:0x28d6b90 *重新使用现有连接! (#0)与主机178.254.13.220 *连接到178.254.13.220(178.254.13.220)端口80(#0)> POST / ocpu / library / DemoPackage / R / addition / json HTTP / 1.1 User-Agent:GuzzleHttp / 6.2 .1 curl / 7.35.0 PHP / 5.5.9-1ubuntu4.20内容类型:application / json主机:178.254.13.220内容长度:67 *上传已完全发送:67个字节中的67个<HTTP / 1.1 400 Bad请求*服务器nginx未列入黑名单<服务器:nginx <日期:星期四,04年4月4日13:29:58 GMT <Content-Type:text / plain; charset = utf-8 <Transfer-Encoding:chunked <Connection:keep-alive <Access-Control-Allow-Origin:* <Access-Control-Expose-Headers:Location,X-ocpu-session,Content-Type,Cache- Control <Access-Control-Allow-Headers:Origin,Content-Type,Accept,Accept-Encoding,Cache-Control,Authorization <Access-Control-Allow-Credentials:true <X-ocpu-r:R version 3.1.3( 2015-03-09)<X-ocpu-locale:en_US.UTF-8 <X-ocpu-time:2019-04-04 15:29:58 CEST <X-ocpu-version:1.4.7 <X-ocpu -server:rApache <Vary:Accept-Encoding <X-Powered-By:PleskLin <* Connection#0到主机178.254.13.220保持不变

php r rstudio guzzle opencpu
3个回答
0
投票

所以你的R服务器返回你HTTP/1.1 400 Bad Request,没有更多的信息。

我只能假设你的PHP代码中的$jsontext包含已经准备好的JSON,所以在这种情况下你需要body选项而不是json'body' => $jsontext)。好像你打包了两次数据。


0
投票

谢谢你的评论。我继续这样调试:在我打电话请求之前

$response = $client->request('POST','http://178.254.13.220/ocpu/library/DemoPackage/R/addition/json', ['json' => $jsontext, 'debug' => true]);

我设置调试R函数内的变量jsontext到一个固定的值,现在函数添加

addition <- function(jsontext)
{
 jsontext <- '{ "jsontext" : [{"a" : "5.1", "b" : "8.9"}]}'     
 ...
 return jsontext
}

现在,结果是正确的,一切都运行良好。因此很明显,我确实通过使用这个表达式以错误的方式在PHP源代码中设置变量$ jsontext:

$data     = '{ "jsontext" : [{"a" : "5.1", "b" : "8.9"}]}' ;
$jsontext = json_decode($data); 
require "vendor/autoload.php";
$client = new GuzzleHttp\Client();
$response = $client->request('POST','http://178.254.13.220/ocpu/library/DemoPackage/R/addition/json', ['json' => $jsontext, 'debug' => true]);

我希望,任何人都熟悉php和R语法,并会帮助我正确的表达式。


0
投票

经过几个小时的调试和阅读几个文档,我可以解决我的问题。 R函数添加的第一行发生错误:

addition <- function(jsontext)
{
 # this was the wrong expression
 tmp <- unlist(jsonlite::fromJSON(jsontext))

 # this is the correct expression
 tmp <- c(unlisted(jsontext))

 ...

 ...

这很奇怪,因为在php源代码中,函数add的参数$ jsontext是以JSON格式设置的

$data     = '{ "jsontext" : [{"a" : "5.1", "b" : "8.9"}]}' ;
$jsontext = json_decode($data);

感谢所有帮助过的人!

© www.soinside.com 2019 - 2024. All rights reserved.