2016-01-11 51 views
0

phpinfo();函數顯示curl已啓用。 dll語句也在.ini文件中取消註釋。 仍然捲曲請求不起作用。PHP/Curl請求不起作用

<?php 
//step1 
$cSession = curl_init(); 
//step2 
curl_setopt($cSession,CURLOPT_URL,"http://www.google.com"); 
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true); 
curl_setopt($cSession,CURLOPT_HEADER, false); 
//step3 
$result=curl_exec($cSession); 
if(curl_errno($cSession)){ 
    echo 'Curl error: ' . curl_error($cSession); 
} 
//step4 
curl_close($cSession); 
//step5 
echo $result; 
?> 

這段代碼顯示了錯誤:

捲曲錯誤:無法連接到192.168.1.1的端口8080:超時

+0

'192.168.1.1'通常是你的路由器的IP?你有沒有在你的HOSTS文件中設計阻止訪問Google – RiggsFolly

+0

我已經使用了這段代碼,它沒有任何問題。您是否在'hosts'文件中拒絕了谷歌? – Vuong

+0

@VictorGeek偉大的思想都相似! – RiggsFolly

回答

-1

使用此代碼

function curl($url) { 
     $ch = curl_init(); // Initialising cURL 
     curl_setopt($ch, CURLOPT_URL, $url); // Setting cURL's URL option with the $url variable passed into the function 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Setting cURL's option to return the webpage data 
     $data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable 
     curl_close($ch); // Closing cURL 
     return $data; // Returning the data from the function 
} 
+0

爲什麼OP應該「試試這個」?一個**好的答案**總是會解釋做了什麼以及爲什麼這樣做, 不僅適用於OP,而且適用於SO的未來訪問者,可能會發現此問題並正在閱讀您的答案。 – RiggsFolly

+0

你還沒有添加任何東西,這將使這個答案。將其轉換爲函數不會解決任何問題 – RiggsFolly

0

您能查看防火牆在那個網站上。我相信該網站上的防火牆會阻止您的服務器IP地址的流量。

檢查DNS解析。更好的是,嘗試從不工作的機器ping 192.168.1.1。另外wget https://192.168.1.1:8080(來自不工作的機器)會給你詳細的錯誤信息。