多年來,我已經看到了使用PHP發佈數據的多種方法,但我很好奇建議的方法是什麼,假設有一種方法。或者,也許有一種有點不言而喻的半公認的做法。這也包括處理響應。將XML張貼到帶有PHP和處理響應的URL
2
A
回答
1
你可以嘗試Snoopy script
它是在託管有用不允許的提供商fopen wrappers
我已經使用它幾年來抓住RSS提要。
1
0
沒有一個真正的標準方法。在用於發佈的代碼中,我通常使用找到的第一個代碼來檢查cURL,file_get_contents和sockets。其中每個支持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);
}
}
1
我喜歡Zend FrameworkZend_Http_Client。
它基本上使用stream_context_create()和stream_socket_client()。
小例子:
$client = new Zend_Http_Client();
$client->setUri('http://example.org');
$client->setParameterPost('foo', 'bar')
$response = $client->request('POST');
$status = $response->getStatus();
$body = $response->getBody();
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);
相關問題
- 1. XML響應處理
- 2. PHP:張貼到URL需要認證
- 3. 將XML響應處理成.html文件
- 4. URL過濾和張貼到MySQL
- 5. 張貼到外部URL不工作用ajax和php捲曲
- 6. 在.NET中處理張貼的JSON值
- 7. PHP cURL API響應處理
- 8. 得到響應代碼400,同時發送xml作爲張貼到Web服務
- 9. PHP請求張貼到IFRAME
- 10. 張貼JSON數據到PHP
- 11. 張貼到Facebook牆 - PHP
- 12. PHP張貼在bitly URL格式
- 13. 來自URL調用的處理響應
- 14. 處理php xml文章和批處理
- 15. 形式張貼到錯誤的URL
- 16. ajaxSubmit會不張貼到完整的URL
- 17. 響應XML - PHP
- 18. URL處理PHP
- 19. 如何處理XML響應空標籤
- 20. 使用XML響應處理GET請求
- 21. windows phone 7 xml響應處理
- 22. Python請求包:處理xml響應
- 23. 處理XML的PHP
- 24. 張貼到Twitter和Facebook
- 25. 張貼到URL掩蓋查詢
- 26. 使用WinJS.xhr張貼變量到URL
- 27. 張貼到URL,但使用Javascript
- 28. 處理與$阿賈克斯(jQuery的)張貼到web服務
- 29. 張貼PHP變量
- 30. 響應處理
哇。我只是看着史努比劇本,它非常簡單。我一定會檢查一下。 – Sampson 2009-01-23 15:24:30
我認爲它甚至包含在一些更大的開源PHP項目中。 – BuddyJoe 2009-01-23 17:14:56