简体   繁体   English

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

[英]php guzzle OpenCPU POST request for a simple R package function stucks during execution

The php guzzle OpenCPU POST request for the simple R DemoPackage function 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]);

stucks during execution on a ubuntu server. 在ubuntu服务器上执行期间卡住了。 The simple addition function adds 2 numbers and returns the sum as result (see code below). 简单的加法函数添加2个数字并返回总和作为结果(参见下面的代码)。

If "POST" is changed to "GET", the request delivers the correct source code of the function addition in the variable body by the command : $body = $response->getBody(); 如果“POST”更改为“GET”,则请求通过以下命令在变量体中提供函数添加的正确源代码:$ body = $ response-> getBody();

On the local Mac computer, where the function was developed and tested, the DemoPackage works fine. 在开发和测试功能的本地Mac计算机上,DemoPackage工作正常。 My other R packages on the ubuntu server run perfect, however I could not find the error in this simple example of adding 2 numbers. 我在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)
 }

The php code sucks while executing the POST request. 执行POST请求时,php代码很糟糕。 With the http request debug option there is an error message, however I am not able to interpret it and or know what to do. 使用http请求调试选项会出现错误消息,但是我无法解释它或知道该怎么做。 The error message is: 错误消息是:

Found bundle for host 178.254.13.220: 0x28d6b90 * Re-using existing connection! 找到主机178.254.13.220的捆绑包:0x28d6b90 *重新使用现有连接! (#0) with host 178.254.13.220 * Connected to 178.254.13.220 (178.254.13.220) port 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 Content-Type: application/json Host: 178.254.13.220 Content-Length: 67 * upload completely sent off: 67 out of 67 bytes < HTTP/1.1 400 Bad Request * Server nginx is not blacklisted < Server: nginx < Date: Thu, 04 Apr 2019 13:29:58 GMT < Content-Type: text/plain; (#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 to host 178.254.13.220 left intact 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保持不变

So your R server returns you HTTP/1.1 400 Bad Request and no more information. 所以你的R服务器返回HTTP/1.1 400 Bad Request ,没有更多信息。

I can only assume that $jsontext in your PHP code contains already prepared JSON, so in this case you need body option instead of json ( 'body' => $jsontext ). 我只能假设你的PHP代码中的$jsontext包含已经准备好的JSON,所以在这种情况下你需要body选项而不是json'body' => $jsontext )。 It seems that you pack you data twice. 好像你打包了两次数据。

Thank you for your remark. 谢谢你的评论。 I continued my debugging this way: Before I called the request 我继续这样调试:在我打电话请求之前

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

I set for debugging the variable jsontext inside the R function to a fixed value and the function addition now looks like 我设置调试R函数内的变量jsontext到一个固定的值,现在函数添加

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

Now, the results are correct and everything runs fine. 现在,结果是正确的,一切都运行良好。 Therefore is seems clear, that I did set the variable $jsontext inside the php source in a wrong way by using this expressions: 因此很明显,我确实通过使用这个表达式以错误的方式在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]);

I hope, that anybody is familiar with the php and R syntax and would help me out with correct expressions. 我希望,任何人都熟悉php和R语法,并会帮助我正确的表达式。

After many hours of debugging and reading several docs I could solve my problem. 经过几个小时的调试和阅读几个文档,我可以解决我的问题。 The error occurred in the first line of the R function addition: R函数添加的第一行发生错误:

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

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

 ...

 ...

It is strange, because in the php source the parameter $jsontext of the function addition was set in the JSON format like 这很奇怪,因为在php源代码中,函数add的参数$ jsontext是以JSON格式设置的

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

Thanks to all who did try to help! 感谢所有帮助过的人!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM