PHP
·
发表于 5年以前
·
阅读量:8414
±¾ÎÄʵÀý½²ÊoÁËPHPÖÐfile_get_contents¸ß¼¶Ó裬ÖÏi¸ø´o¼Ò¹(C)´o¼Ò²Î¿¼¡£¾ßÌa*ÖÎoÈçÏ£º
Ê×ÏȽa¾ofile_get_contentsµÄ³¬Ê±ÎÊÌa£¬ÔÚ³¬Ê±µ»Ø´iÎoºo¾ÍÏojsÖеÄsettimeoutÄÇÑu½øÐÐÒ»´Î³¢ÊÔ£¬´iÎo³¬¹ý3´Î»oÕß5´Îºo¾ÍÈÈÏΪÎÞ¨Á¬ÏßËÅþÆ÷¶ø³¹µ×ÅÆu¡£
ÕaÑe¾Í¼oµ¥½eÉÜÁ½ÖÖ½a¾o½*¨£º
Ò»¡¢Ôo¼Ó³¬Ê±µÄʱ¼aÏÞÖÆ
×¢Òa£ºset_time_limitÖ»ÊÇÉe¶¨ÄaµÄPHP³ÌʽµÄ³¬Ê±Ê±¼a£¬¶ø²»ÊÇfile_get_contentsº¯Êý¶ÁÈ¡URLµÄ³¬Ê±Ê±¼a¡£
ÎÒÒ»¿ªÊ¼ÒÔΪset_time_limitÒ²ÄÜÓ°Ïiµ½file_get_contents£¬ºoÀ´¾²aÊÔÊÇÎÞЧµÄ¡£ÕaeÕýµÄÐÞ¸Äfile_get_contentsÑÓʱ¿ÉÒÔÓÃresource $contextµÄtimeout²ÎÊý£º
PHP³ÌÐo´uÂeÈçÏ£º
$opts = array(
'http'=>array(
'method'=>"GET",
'timeout'=>60,
)
);
$context = stream_context_create($opts);
$html =file_get_contents('http://www.jb51.net', false, $context);
fpassthru($fp);
¶þ¡¢¶a´Î³¢ÊÔ
PHP³ÌÐo´uÂeÈçÏ£º
$cnt=0;
while($cnt < 3 && ($str=@file_get_contents('http...'))===FALSE){
$cnt++;
}
ÒÔÉϽ¨¶Ô¸¶³¬Ê±ÒѾOKÁË¡£½ÓÏÂÀ´ÑÝʾһÏÂÓÃfile_get_contentsʵÏÖPost£¬ÈçÏ£º
PHP³ÌÐo´uÂe
function Post($url, $post = null){
$context = array();
if (is_array($post)) {
ksort($post);
$context['http'] = array (
'timeout'=>60,
'method' => 'POST',
'content' => http_build_query($post, '', '&'),
);
}
return file_get_contents($url, false, stream_context_create($context));
}
$data = array (
'name' => 'test',
'email' => 'test@gmail.com',
'submit' => 'submit',
);
echo Post('http://www.jb51.net', $data);
×¢ÒaÎĵµÍµÄSet_time_outñÔoÕu¸oÎĵµ¶¼µÃ³¬Ê±ÁË¡£
Ï£Íu±¾ÎÄËuÊo¶Ô´o¼Òphp³ÌÐoÉe¼ÆµÄѧϰÓÐËu°iÖu¡£