【问题标题】:php guzzle OpenCPU POST request for a simple R package function stucks during executionphp guzzle 一个简单的 R 包函数的 OpenCPU POST 请求在执行期间卡住了
【发布时间】:2019-08-24 18:13:25
【问题描述】:

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

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

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

如果“POST”改为“GET”,则请求通过以下命令在变量body中传递正确的函数添加源代码:$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 用户代理:GuzzleHttp/6.2 .1 curl/7.35.0 PHP/5.5.9-1ubuntu4.20 内容类型:application/json 主机:178.254.13.220 内容长度:67 * 上传完全发送:67 个字节中的 67 个

【问题讨论】:

  • 信息不足,无法提供帮助。请在启用 debug 选项的情况下运行您的 HTTP 请求并添加结果。
  • 一个好建议!我添加了调试选项(见上文)并更改了添加函数的前 3 行。

标签: php r rstudio guzzle opencpu


【解决方案1】:

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

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

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

 ...

 ...

很奇怪,因为在php源码中,函数添加的参数$jsontext被设置成json格式之类的

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

感谢所有尝试提供帮助的人!

【讨论】:

    【解决方案2】:

    感谢您的评论。我以这种方式继续调试:在我调用请求之前

    $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 语法,并能帮助我正确表达。

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多