gpt4 book ai didi

php - 与openCPU交互

转载 作者:行者123 更新时间:2023-12-04 18:14:52 24 4
gpt4 key购买 nike

我偶然发现了一个很棒的开源项目 openCPU.org,我对这个项目感到非常兴奋。作为一名试图创建一个网站来托管我的工作的研究科学家,我最希望能够在云上运行 R 以让我的脚本实时运行并显示在我的网页上。非常感谢 Jeroen 促成了这个项目。

有了这个,我的问题。

我到底如何与 openCPU 交互?

我可以将示例函数放入“运行一些代码”中:

http://public.opencpu.org/userapps/opencpu/opencpu.demo/runcode/

并检索我的代码的 PNG 图像,这很棒!

但是如何在我自己的网页中或通过 URL 执行此操作?

我可以从此页面获取原始代码上传的对象,例如:“x3ce3bf3e33”

如果是类似的函数:

myfun <-function(){

x = seq(1,6.28)
y = cos(x)
p = plot(x,y)
print(p)
# also tried return(p)
}

我不应该可以通过以下方式调用它:

http://public.opencpu.org/R/tmp/x3ce3bf3e33/png

输入变量呢?例如。:
myfun <-function(foo){

x = seq(1,foo)
y = cos(x)
p = plot(x,y)
print(p)
}

我觉得也许我缺少一些东西。如何使用 url 指定“GET”或“POST”?

编辑

好的,为了响应下面的@Jeroen,我需要使用 POST 和 GET 和 API。现在我的问题延伸到以下问题,即让 PHP 与它正确交互。

说我有代码:
<?php

$foo = 'bar';
$options = array(
'method' => 'POST',
'foo' => $foo,
);
$url = "http://public.opencpu.org/R/tmp/x0188b9b9ce/save";
$result = drupal_http_request($url,$options); // drupal function
?>

然后我如何访问 $result 中传回的内容?我正在寻找一张图表。它看起来像这样:
{
"object" : null,
"graphs" : [
"x2acba9501a"
],
"files" : {}
}

下一步将是获取图像,类似于:
$newurl = "http://public.opencpu.org/R/tmp/".$result["graph"]."/png";
$image = drupal_http_request($newurl);
echo $image;

但我不知道如何访问 $result 的各个元素?

编辑#2

好的,伙计们,我已经完成了这项工作,这要归功于下面的答案和其他多个帮助 session ,以及很多我的头撞在显示器上。

我们开始了,用 cURL 完成
<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://public.opencpu.org/R/tmp/x0188b9b9ce/save');
curl_setopt($ch, CURLOPT_POST, 1); // Method is "POST"
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Returns the curl_exec string, rather than just Logical value
$result = curl_exec($ch);
curl_close($ch);
$new = json_decode($result,true); // $result is in 'json' format, decode it
$get = $new['graphs'][0]; // the 'hashkey for the image, "x2acba9501a" above

$img = 'http://public.opencpu.org/R/tmp/'.$get.'/png'; // link to the png image

echo <<<END // use this to display an image from the url
<a href="$img">
<img src="$img">
</a>
END

?>

最佳答案

OpenCPU 使用 HTTP POST 执行函数,使用 HTTP GET 读取/渲染对象和图形。您可以先将函数保存到临时存储,然后从那里调用它。 interactive manual 的“/R/tmp API”一章中给出了一个基本示例。 .如果单击名为 save a function 的红色演示按钮, get the functioncall the function它将帮助您完成这些步骤。

基本上在第一步中,您对身份函数执行 HTTP POST 以将您的函数保存在存储中。这也是 running code example page 的第三种形式正在执行的操作。你发现的。所以我只是在那里复制了你的代码,然后它返回了我的对象 x0188b9b9ce .

要检查一切是否正常,您可以使用 HTTP GET 读取此对象。例如,打开这个 url 来阅读你的函数的源代码:

http://public.opencpu.org/R/tmp/x0188b9b9ce/ascii

例如,替代输出是将函数作为 RData 文件获取:
http://public.opencpu.org/R/tmp/x0188b9b9ce/rda

重要的是 HTTP GET 从不执行函数。它只是查找内容,并以您请求的输出格式返回。所以现在你确信你的函数在我们想要运行的 store 中。为此,您需要再次进行 HTTP POST。例如要获取 PDF,您可以执行
POST http://public.opencpu.org/R/tmp/x0188b9b9ce/pdf
POST http://public.opencpu.org/R/tmp/x0188b9b9ce/svg
POST http://public.opencpu.org/R/tmp/x0188b9b9ce/png

如果您调用的函数接受参数,则将它们作为参数包含在 HTTP POST 请求中。当您想在您的网页中包含输出时,通常您只想将 HTTP POST 与 /save 结合使用。输出类型。因此,您将使用 jquery 或其他任何方法:
POST http://public.opencpu.org/R/tmp/x0188b9b9ce/save

这可能会返回如下内容:
{
"object" : null,
"graphs" : [
"x2acba9501a"
],
"files" : {}
}

这表明您的函数已成功执行,并且它创建了一个绘图(耶!)。图形被保存到 tmp 存储。因此,您现在可以使用 HTTP GET 获取图形并将其嵌入到您的页面中:
http://public.opencpu.org/R/tmp/x2acba9501a/png
http://public.opencpu.org/R/tmp/x2acba9501a/png?!width=900&!height=500
http://public.opencpu.org/R/tmp/x2acba9501a/pdf
http://public.opencpu.org/R/tmp/x2acba9501a/svg

关于php - 与openCPU交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11927829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com