2010-12-05 26 views
55

मैं URL का उपयोग करने के लिए file_get_contents() का उपयोग कर रहा हूं।file_get_contents जब url मौजूद नहीं है

file_get_contents('http://somenotrealurl.com/notrealpage'); 

यदि यूआरएल वास्तविक नहीं है, तो यह त्रुटि संदेश लौटाता है। मैं इसे गलती से गलती से कैसे प्राप्त कर सकता हूं ताकि मुझे पता चले कि पृष्ठ मौजूद नहीं है और इस त्रुटि संदेश को प्रदर्शित किए बिना तदनुसार कार्य करता है?

file_get_contents('http://somenotrealurl.com/notrealpage') 
[function.file-get-contents]: 
failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found 
in myphppage.php on line 3 

Zend में उदाहरण के लिए आप कह सकते हैं: if ($request->isSuccessful())

$client = New Zend_Http_Client(); 
$client->setUri('http://someurl.com/somepage'); 

$request = $client->request(); 

if ($request->isSuccessful()) { 
//do stuff with the result 
} 
+0

धारा संदर्भ का उपयोग करके देखें: http://stackoverflow.com/questions/21800276/is-it-possible-to-get-404-page-content-using-fopen-in-php, file_get_contents fopen का उपयोग करता है हुड के नीचे। – rsk82

उत्तर

87

आप HTTP response code जांच करने की आवश्यकता संभाल कर सकते हैं:

function get_http_response_code($url) { 
    $headers = get_headers($url); 
    return substr($headers[0], 9, 3); 
} 
if(get_http_response_code('http://somenotrealurl.com/notrealpage') != "200"){ 
    echo "error"; 
}else{ 
    file_get_contents('http://somenotrealurl.com/notrealpage'); 
} 
+4

यह तकनीक मेरे लिए बेहतर है अगर आपको पता होना चाहिए कि अनुरोध क्यों विफल हुआ, यानी। स्टेटस कोड की जांच (उदाहरण के लिए 404 को अलग-अलग 503 तक संभालने की आवश्यकता हो सकती है)। यदि नहीं, तो यह संभावित रूप से दो अनुरोध प्रस्तुत करता है और अनदेखा तब बेहतर होता है। – Orbling

+1

हालांकि यह एक अच्छा समाधान है, यह 500 जैसे अन्य http त्रुटि कोडों पर विचार नहीं करता है। इसलिए, एक साधारण ट्विक इस तरह हो सकता है: '$ हेडर = get_headers ($ uri);' 'if (stripos ($ headers [0], '40')! == झूठी || स्ट्रिपोज़ ($ हेडर [0], '50')! == झूठी) {' ' ... त्रुटियों को संभालें ... ' '} ' – YOMorales

+11

मुझे लगता है कि यह कोड गलत है। आपको 'get_headers' केवल तभी कॉल करना चाहिए यदि' file_get_contents' 'false' लौटाता है। यह हर यूआरएल को दो बार कॉल करने के लिए ज्यादा समझ में नहीं आता है। सिवाय आप उम्मीद करते हैं कि आपके अधिकांश यूआरएल असफल हो जाएंगे। यह वास्तव में दुखी है कि स्थिति $ 4xx या 5xx होने पर '$ http_response_header' खाली है। इसके द्वारा हमें 'get_headers' की आवश्यकता नहीं होगी। – mgutt

46

PHP में इस तरह के आदेशों के साथ, आप इस तरह के चेतावनी को दबाने के लिए एक @ के साथ उन्हें उपसर्ग कर सकते हैं।

@file_get_contents('http://somenotrealurl.com/notrealpage'); 

file_get_contents() रिटर्न FALSE एक विफलता, तब होता है, इसलिए यदि आप उस के खिलाफ प्राप्त परिणाम की जांच तो आप विफलता

$pageDocument = @file_get_contents('http://somenotrealurl.com/notrealpage'); 

if ($pageDocument === false) { 
    // Handle error 
} 
+3

मैं सिर्फ त्रुटियों को दबाना नहीं चाहता हूं। मैं जानना चाहता हूं कि यूआरएल वैध है या नहीं। – sami

+0

ध्यान दें कि यदि सर्वर नीचे है तो फ़ंक्शन थोड़ी देर के लिए अवरुद्ध हो सकता है। –

+0

@sami जब आप 'मान्य' कहते हैं, तो क्या आपका मतलब वैध यूआरएल है, या "काम करता है"? – Orbling

13

जबकि file_get_contents बहुत तेज़ और सुविधाजनक है, मैं बेहतर नियंत्रण के लिए कर्ल लाइब्रेरी का पक्ष लेता हूं। यहां एक उदाहरण दिया गया है।

function fetchUrl($uri) { 
    $handle = curl_init(); 

    curl_setopt($handle, CURLOPT_URL, $uri); 
    curl_setopt($handle, CURLOPT_POST, false); 
    curl_setopt($handle, CURLOPT_BINARYTRANSFER, false); 
    curl_setopt($handle, CURLOPT_HEADER, true); 
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 10); 

    $response = curl_exec($handle); 
    $hlength = curl_getinfo($handle, CURLINFO_HEADER_SIZE); 
    $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); 
    $body  = substr($response, $hlength); 

    // If HTTP response is not 200, throw exception 
    if ($httpCode != 200) { 
     throw new Exception($httpCode); 
    } 

    return $body; 
} 

$url = 'http://some.host.com/path/to/doc'; 

try { 
    $response = fetchUrl($url); 
} catch (Exception $e) { 
    error_log('Fetch URL failed: ' . $e->getMessage() . ' for ' . $url); 
} 
+0

ऐ, कर्ल लाइब्रेरी बहुत बेहतर है - मैंने कभी भी 'file_get_contents()' के साथ यूआरएल नहीं लाया, मुझे इस तरह के स्ट्रीम रैपर का उपयोग करना पसंद नहीं है, थोड़ा सा झटका लगता है। – Orbling

22

हर बार जब आप एक http आवरण के साथ file_get_contents कहते हैं, स्थानीय दायरे में एक चर बनाई गई है: $http_response_header

यह चर सभी HTTP हेडर हैं। यह विधि get_headers() फ़ंक्शन से बेहतर है क्योंकि केवल एक अनुरोध निष्पादित किया गया है।

नोट: 2 अलग-अलग अनुरोध अलग-अलग समाप्त हो सकते हैं। उदाहरण के लिए, get_headers() 503 लौटाएगा और file_get_contents() 200 लौटाएगा। और आपको उचित आउटपुट मिलेगा लेकिन get_headers() कॉल में 503 त्रुटि के कारण इसका उपयोग नहीं किया जाएगा।

function getUrl($url) { 
    $content = file_get_contents($url); 
    // you can add some code to extract/parse response number from first header. 
    // For example from "HTTP/1.1 200 OK" string. 
    return array(
      'headers' => $http_response_header, 
      'content' => $content 
     ); 
} 

// Handle 40x and 50x errors 
$response = getUrl("http://example.com/secret-message"); 
if ($response['content'] === FALSE) 
    echo $response['headers'][0]; // HTTP/1.1 401 Unauthorized 
else 
    echo $response['content']; 

इस aproach भी alows आप कुछ अनुरोध अलग चर में संग्रहीत के बाद से यदि आप file_get_contents() का उपयोग $http_response_header स्थानीय दायरे में ओवरराइट है हेडर का ट्रैक है।

+0

यह सही है, यह तथ्य है कि यह अतिरिक्त अनुरोध सहेजता है मेरा +1 प्राप्त करता है .. मैं हजारों यूआरएल के कैश उत्पन्न करने के साथ काम कर रहा हूं .. इसलिए अनुरोधों पर डबल अप करना होगा हास्यास्पद होगा। – jenovachild

3

सरल और कार्यात्मक (आसान कहीं भी उपयोग करने के लिए):

function file_contents_exist($url, $response_code = 200) 
{ 
    $headers = get_headers($url); 

    if (substr($headers[0], 9, 3) == $response_code) 
    { 
     return TRUE; 
    } 
    else 
    { 
     return FALSE; 
    } 
} 

उदाहरण:

$file_path = 'http://www.google.com'; 

if(file_contents_exist($file_path)) 
{ 
    $file = file_get_contents($file_path); 
} 
3

डबल अनुरोध से बचने के लिए के रूप में ynh का जवाब आप उनके जवाब जोड़ सकता पर Orbling द्वारा टिप्पणी की । यदि आपको पहले स्थान पर वैध प्रतिक्रिया मिलती है, तो इसका उपयोग करें। अगर पता नहीं था कि समस्या क्या थी (यदि आवश्यक हो)।

$urlToGet = 'http://somenotrealurl.com/notrealpage'; 
$pageDocument = @file_get_contents($urlToGet); 
if ($pageDocument === false) { 
    $headers = get_headers($urlToGet); 
    $responseCode = substr($headers[0], 9, 3); 
    // Handle errors based on response code 
    if ($responseCode == '404') { 
     //do something, page is missing 
    } 
    // Etc. 
} else { 
    // Use $pageDocument, echo or whatever you are doing 
}