2009-01-23 30 views
2

多年來,我已經看到了使用PHP發佈數據的多種方法,但我很好奇建議的方法是什麼,假設有一種方法。或者,也許有一種有點不言而喻的半公認的做法。這也包括處理響應。將XML張貼到帶有PHP和處理響應的URL

回答

1

你可以嘗試Snoopy script
它是在託管有用不允許的提供商fopen wrappers
我已經使用它幾年來抓住RSS提要。

+0

哇。我只是看着史努比劇本,它非常簡單。我一定會檢查一下。 – Sampson 2009-01-23 15:24:30

+0

我認爲它甚至包含在一些更大的開源PHP項目中。 – BuddyJoe 2009-01-23 17:14:56

0

沒有一個真正的標準方法。在用於發佈的代碼中,我通常使用找到的第一個代碼來檢查cURL,file_get_contentssockets。其中每個支持GET和POST,根據PHP的版本和配置,每個可能或不可用(或工作)。

基本上是這樣的:

function do_post($url, $data) { 
    if (function_exists('curl_init') && ($curl = curl_init($url))) { 
    return do_curl_post($curl, $data); 
    } else if (function_exists('file_get_contents') && ini_get('allow_url_fopen') == "1") { 
    return do_file_get_contents_post($url, $data); 
    } else { 
    return do_socket_post($url, $data); 
    } 
} 
3

雖然史努比腳本也許很酷,如果你正在尋找只是PHP發佈XML數據,爲什麼不使用捲曲?這很容易,有錯誤處理,並且已經在你的包裏成爲一個有用的工具。以下是如何在PHP中使用cURL將XML發佈到URL的示例。

// url you're posting to   
$url = "http://mycoolapi.com/service/"; 

// your data (post string) 
$post_data = "first_var=1&second_var=2&third_var=3"; 

// create your curl handler  
$ch = curl_init($url); 

// set your options  
curl_setopt($ch, CURLOPT_MUTE, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //ssl stuff 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

// your return response 
$output = curl_exec($ch); 

// close the curl handler 
curl_close($ch);