我偶然發現了一個名爲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