我偶然发现了一个名为openCPU.org的令人敬畏的开源项目,我对这个项目感到非常兴奋。作为一名研究科学家,我试图创建一个托管我的工作的网站,我只想在云上运行R,让我的脚本实时运行并显示在我的网页上。所以感谢Jeroen让这个项目发生了很长时间。与openCPU交互
有了这个,我的问题。
我该如何与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并与该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
?>
我怎么那么访问哪些传递回来$结果?我正在寻找一个图表。它看起来像这样:
{
"object" : null,
"graphs" : [
"x2acba9501a"
],
"files" : {}
}
下一个步骤将是获得图像,沿着线的东西:
$newurl = "http://public.opencpu.org/R/tmp/".$result["graph"]."/png";
$image = drupal_http_request($newurl);
echo $image;
但我不知道如何访问$结果中的单个元素?
编辑#2
玉家伙,我已经得到了这个工作,由于下方和多个其他帮助会话的答案,很多砸我的头监视器。
在这里,我们走了,带着卷曲
<?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
?>
你的问题有几个问题:你的第一个编辑可能应该是一个单独的问题,因为它非常独立的,第二,你的第二个编辑绝对应该是一个答案,而不是一个问题。请记住,stackoverflow不应该是一个论坛,而是一个问答网站,这里的问题的真正答案:如何让opencpu与php交互隐藏在问题中... – cmbarbu 2015-08-12 09:11:13