2011-12-16 21 views
28

Belirli sayfamda harici bağlantılar almak ve göstermek için file_get_contents işlevini kullanıyorum. Benim yerel dosya her şeyDosya_get_contents yerine CURL nasıl kullanılır?

tamam, ama benim sunucu file_get_contents işlevini desteklemiyorsa, bu yüzden aşağıda kodu ile cURL kullanmaya çalıştı:

function file_get_contents_curl($url) { 
    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_URL, $url); 

    $data = curl_exec($ch); 
    curl_close($ch); 

    return $data; 
} 

echo file_get_contents_curl('http://google.com'); 

Ama boş bir sayfa döndürür. Yanlış olan ne?

+3

[curl_error] (http://php.net/manual/en/function.curl-error.php) ne diyor? –

+2

kodlamanız çalışıyor, belki de curl yüklenmiyor mu? phpinfo() – malletjo

+3

'da kontrol edin. Hata kontrolü yapmıyorsunuz ve neden hiç hata çıkmadığını merak ediyorsunuz. Bu .... doğru değil. –

cevap

68

bu deneyin:

function file_get_contents_curl($url) { 
    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);  

    $data = curl_exec($ch); 
    curl_close($ch); 

    return $data; 
} 
+1

Sadece öğrenme curl. Bu harika teşekkürler! –

+0

Açık bir çözümdür teşekkür ederim. –

+0

Curl boş İçeriği döndürür .. bundan nasıl kurtulabilirim? – 151291

8

Bu

function curl_load($url){ 
    curl_setopt($ch=curl_init(), CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $response = curl_exec($ch); 
    curl_close($ch); 
    return $response; 
} 

$url = "http://www.google.com"; 
echo curl_load($url); 
+2

Bu kod, tam olarak file_get_contents gibi davranmayacaktır. Kodunuz yönlendirmeleri takip etmez, file_get_contents bunu yapar. –

1

çalışmalıdır // Bu deneyebilirsiniz. İyi çalışmalı.

function curl_tt($url){ 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);  
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
$data = curl_exec($ch); 
curl_close($ch); 

return $data; 
} 
echo curl_tt("https://google.com"); 
İlgili konular