2012-02-26 44 views
2

我有一些問題讓cURL在PHP中工作。我是一個完整的初學者(截至前幾天)與PHP。PHP初學者 - 有cURL問題

基本上,我試圖從我的Arduino輸出(每10秒輸出一次溫度)並將其輸入到我的網頁。計劃是將數據存儲在某種數據庫中,以便我可以分析歷史/情節圖等。

現在我只需要將數據傳入即可。

的Arduino是這裏還有隨地吐痰的溫度,沒事的,我的路由器是要做出可以在網絡上 - http://2.216.137.236/

我的代碼如下:

<?php 

$Url = "http://2.216.137.236/"; 
echo "Hello, is this on?" . "<br>"; 
// is cURL installed? 
if (!function_exists('curl_init')){ 
    echo "cURL not installed!"; 
    die('Sorry cURL is not installed!'); 
} 
echo "cURL is installed!" . "<br>"; 

// OK cool - then let's create a new cURL resource handle 
$ch = curl_init(); 

// Now set some options (most are optional) 

// Set URL to download 
curl_setopt($ch, CURLOPT_URL, $Url); 

// Include header in result? (0 = yes, 1 = no) 
curl_setopt($ch, CURLOPT_HEADER, 1); 

// Should cURL return or print out the data? (true = return, false = print) 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

// Timeout in seconds 
curl_setopt($ch, CURLOPT_TIMEOUT, 10); 

// Download the given URL, and return output 
$output = curl_exec($ch); 

// Close the cURL resource, and free system resources 
curl_close($ch); 

print_r ($output); 

>

但是現在我所看到的全部是 「你好,這是嗎? cURL已安裝!」 沒有溫度的任何地方標誌(見這裏 - http://wetdreams.org.uk/ChrisProject/curl_test.php) (原諒的URL,我的獨木舟網站)

我真的希望沒有被殺死了我一些n00by PHP的事情。

無論如何,希望你能幫助我。

編輯︰我可憐的小路由器已經崩潰試圖爲您的所有請求服務,事後看來可能不應該把IP了。將重置它並再試一次,之後的鏈接可能不起作用。

回答

0

最終這article可能會幫助你。

簡單的方法是使用file_get_contents("url"),但你當然可以使用捲曲,如果你想。

0

我想你的代碼,和在添加了此:

echo curl_getinfo($ch, CURLINFO_HTTP_CODE); 

if(curl_exec($ch) === false) 
{ 
    echo 'Curl error: ' . curl_error($ch); 
} 
else 
{ 
    echo 'Operation completed without any errors'; 
} 

的HTTP代碼返回0,錯誤是:捲曲錯誤:從服務器空回覆。

我不能說這是什麼原因,但它應該有助於進一步調試。 (我可以在我的瀏覽器中看到網頁的temp輸出 - 所以我真的不明白爲什麼這個頁面沒有被提供給Curl)

+0

嘿,謝謝,我認爲我的路由器自從它剛剛家庭路由器。一旦它重新設置,將嘗試這一點,並回到你身邊。 – 2012-02-26 12:10:27

+0

好的,只是讓你知道它在你的路由器崩潰之前就已經返回了(它在我的瀏覽器中查看時仍然給我一個臨時的)。 – BenOfTheNorth 2012-02-26 14:29:38