回答
只是用下面一段代碼從RESTful Web服務URL的響應,我使用社交提到的網址,
$response = get_web_page("http://socialmention.com/search?q=iphone+apps&f=json&t=microblogs&lang=fr");
$resArr = array();
$resArr = json_decode($response);
echo "<pre>"; print_r($resArr); echo "</pre>";
function get_web_page($url) {
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_ENCODING => "", // handle compressed
CURLOPT_USERAGENT => "test", // name of client
CURLOPT_AUTOREFERER => true, // set referrer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // time-out on connect
CURLOPT_TIMEOUT => 120, // time-out on response
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
過時的歌曲的bug一個禮包... +1使用curl_setopt_array()。比反覆調用curl_setopt()更清潔。 – Ligemer
解決方案的核心是設定
CURLOPT_RETURNTRANSFER => true
然後
$response = curl_exec($ch);
CURLOPT_RETURNTRANSFER告訴PHP將響應存儲在變量中而不是打印出來到頁面,所以$迴應將包含您的迴應。這是你最基礎的工作代碼(我認爲,沒有測試):
// init curl object
$ch = curl_init();
// define options
$optArray = array(
CURLOPT_URL => 'http://www.google.com',
CURLOPT_RETURNTRANSFER => true
);
// apply those options
curl_setopt_array($ch, $optArray);
// execute request and get response
$result = curl_exec($ch);
給點答案。解決了我的問題。非常感謝你。 –
很好的解釋。 – Kailas
如果任何人遇到這樣,我加入另一個答案提供可能在需要響應代碼或其他信息響應」。
http://php.net/manual/en/function.curl-getinfo.php
// init curl object
$ch = curl_init();
// define options
$optArray = array(
CURLOPT_URL => 'http://www.google.com',
CURLOPT_RETURNTRANSFER => true
);
// apply those options
curl_setopt_array($ch, $optArray);
// execute request and get response
$result = curl_exec($ch);
// also get the error and response code
$errors = curl_error($ch);
$response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
var_dump($errors);
var_dump($response);
// output
string(0) ""
int(200)
// change www.google.com to www.googlebofus.co
string(42) "Could not resolve host: www.googlebofus.co"
int(0)
不錯,我不知道curl_error() – siliconrockstar
- 1. 如何使用curl獲得JSON響應
- 2. 如何在php curl函數中獲得mime類型的響應
- 3. 嘗試使用curl獲得響應exec
- 4. 如何獲得curl響應以在php中獲取特定標記?
- 5. 使用PHP cURL可以獲得部分響應嗎?
- 6. 如何使用curl和shell腳本從https url獲得響應
- 7. 如何使用php在curl xml響應中顯示值?
- 8. 在PHP中使用CURL,FOPEN或FILE_GET_CONTENTS獲取XML響應
- 9. 我該如何使用我在cURL中從腳本本身獲得的響應
- 10. cURL爲IIS站點獲得403響應
- 11. 用cURL使用imgur API獲得空白響應
- 12. 如何使用ruby獲得JSON響應
- 13. 如何使用Javamail獲得SMTP響應
- 14. CURL使用PHP不顯示響應
- 15. 使用PHP和Curl隱藏響應
- 16. 在javascript中獲得響應PHP
- 17. 從PHP獲取頭文件cURL響應
- 18. 從響應curl獲取參數php
- 19. 使用curl獲取URL響應的值
- 20. VIMEO(Pro)獲得JSON響應幫助(PHP/CURL)
- 21. PHP Curl Recaptcha無法獲得響應成功屬性
- 22. 使用cURL,file_get_contents等從服務器獲得響應的問題
- 23. 如何在webview中獲得json響應
- 24. 如何在Struts2中獲得Ajax響應
- 25. 如何在Groovy中獲得REST響應?
- 26. 如何在QtWebKit中獲得響應
- 27. 如何捕獲CURL的響應
- 28. Curl沒有在PHP中給出響應
- 29. Curl以空字符串響應。如何獲得更多信息?
- 30. 如何獲得不同頁面的cURL響應?
檢查此鏈接http://www.jonasjohn.de/snippets/php/curl-example.htm – mymotherland