我有一个页面,我从curl中检索许多网址中的内容。我发现在检索到一定数量的url后,curl返回null。 curl将在任何给定会话中检索的资源是否有限制。
我在检索每个网址之前执行curl_init()
,在获取内容后执行curl_close()
。我认为这将立即释放资源可以防止达到极限的情况。调用curl_init()
次数有问题。
以下是我检索网址的PHP代码示例。
<?php
function get_curl() {
// Initialize a new curl resource
$ch = curl_init();
// Get only the content not the headers
curl_setopt($ch, CURLOPT_HEADER, 0);
// Return the value instead of printing the response to browser
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Use a user agent to mimic a browser
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');
return $ch;
// Remember to always close the session and free all resources
}
// Make sure curl is installed
$curl_exists = function_exists('curl_init');
if ( !$curl_exists ) {
return;
}
$todayfeast = date("Ymd");
$ctitleff = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=FR';
$creadff = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading&lang=FR&content=FR';
$ch = get_curl();
curl_setopt($ch, CURLOPT_URL, $ctitleff);
$titleff = curl_exec($ch);
curl_close($ch);
$ch = get_curl();
curl_setopt($ch, CURLOPT_URL, $creadff);
$readff = curl_exec($ch);
curl_close($ch);
$ctitlepf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=PS';
$creadpf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&&type=reading&lang=FR&content=PS';
$ch = get_curl();
curl_setopt($ch, CURLOPT_URL, $ctitlepf);
$titlepf = curl_exec($ch);
curl_close($ch);
$ch = get_curl();
curl_setopt($ch, CURLOPT_URL, $creadpf);
$readpf = curl_exec($ch);
curl_close($ch);
$titlesf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=SR';
$readsf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading&lang=FR&content=SR';
$ch = get_curl();
curl_setopt($ch, CURLOPT_URL, $titlesf);
$titlesf = curl_exec($ch);
curl_close($ch);
$ch = get_curl();
curl_setopt($ch, CURLOPT_URL, $readsf);
$readsf = curl_exec($ch);
curl_close($ch);
$titlegf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=GSP';
$readgf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading&lang=FR&content=GSP';
$ch = get_curl();
curl_setopt($ch, CURLOPT_URL, $titlegf);
$titlegf = curl_exec($ch);
curl_close($ch);
$ch = get_curl();
curl_setopt($ch, CURLOPT_URL, $readgf);
$readgf = curl_exec($ch);
curl_close($ch);
// Send info
$params = array(
'titleff' => $titleff,
'readff' => $readff,
'titlepf' => $titlepf,
'readpf' => $readpf,
'titlesf' => $titlesf,
'readsf' => $readsf,
'titlegf' => $titlegf,
'readgf' => $readgf,
);
echo json_encode($params);
?>
修改
问题只发生在我通过ajax调用这个php脚本时。
<script>
$('#readingf').ready(function(){
$.ajax({
url: "loadfrench.php",
type: "GET",
data: { },
cache: false,
async: true,
dataType: 'json',
success: function (response) {
if (response != '')
{
$('#titleff').html(response.titleff);
$('#readff').html(response.readff);
$('#titlepf').html(response.titlepf);
$('#readpf').html(response.readpf);
$('#titlesf').html(response.titlesf);
$('#readsf').html(response.readsf);
$('#titlegf').html(response.titlegf);
$('#readgf').html(response.readgf);
alert ('titleff '+response.titleff+' readff '+response.readff);
}
},
error: function (request, status, error) {
alert ("status "+status+" error "+error+"responseText "+request.responseText);
},
});
});
</script>
如果我改为使用此文件,并在<?php require("filename.php") ?>
的主页中加入,则可以正常使用。
<?php
function get_curl() {
// Initialize a new curl resource
$ch = curl_init();
// Get only the content not the headers
curl_setopt($ch, CURLOPT_HEADER, 0);
// Return the value instead of printing the response to browser
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Use a user agent to mimic a browser
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');
return $ch;
// Remember to always close the session and free all resources
}
// Make sure curl is installed
$curl_exists = function_exists('curl_init');
if ( !$curl_exists ) {
return;
}
$ch = get_curl();
$todayfeast = date("Ymd");
/* $titleff = $readff = $titlepf = $readpf = $titlesf = $readsf = $titlegf = $readgf = "blank"; */
$ctitleff = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=FR';
$creadff = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading&lang=FR&content=FR';
curl_setopt($ch, CURLOPT_URL, $ctitleff);
$titleff = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, $creadff);
$readff = curl_exec($ch);
$ctitlepf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=PS';
$creadpf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&&type=reading&lang=FR&content=PS';
curl_setopt($ch, CURLOPT_URL, $ctitlepf);
$titlepf = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, $creadpf);
$readpf = curl_exec($ch);
$titlesf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=SR';
$readsf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading&lang=FR&content=SR';
curl_setopt($ch, CURLOPT_URL, $titlesf);
$titlesf = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, $readsf);
$readsf = curl_exec($ch);
$titlegf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=GSP';
$readgf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading&lang=FR&content=GSP';
curl_setopt($ch, CURLOPT_URL, $titlegf);
$titlegf = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, $readgf);
$readgf = curl_exec($ch);
curl_close($ch);
// Send info
$params = array(
'titleff' => $titleff,
'readff' => $readff,
'titlepf' => $titlepf,
'readpf' => $readpf,
'titlesf' => $titlesf,
'readsf' => $readsf,
'titlegf' => $titlegf,
'readgf' => $readgf,
);
echo json_encode($params);
?>
<?php
$curl_exists = function_exists('curl_init');
// Make sure curl is installed
if ( $curl_exists ) {
// Initialize a new curl resource
$ch = curl_init();
// Get only the content not the headers
curl_setopt($ch, CURLOPT_HEADER, 0);
// Return the value instead of printing the response to browser
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Use a user agent to mimic a browser
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');
}
?>
<div id="readfre" style="overflow:hidden;">
<div id="readingf" class="tabber" style="width:100%; margin-top: 10px; margin-right:0px;">
<?php
$todayfeast = date("Ymd");
$ctitleff = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=FR';
$creadff = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading&lang=FR&content=FR';
if ( $curl_exists ) {
curl_setopt($ch, CURLOPT_URL, $ctitleff);
$titleff = curl_exec($ch);
}
if ( $curl_exists ) {
curl_setopt($ch, CURLOPT_URL, $creadff);
$readff = curl_exec($ch);
}
?>
<div id="readf" class="tabbertab">
<h2>PremiËre Lecture</h2>
<div id='titleff' class='rtitle'>
<?php echo $titleff; ?>
</div>
<div id='readff' class='rtext'>
<?php echo $readff; ?>
</div>
</div>
<?php
$ctitlepf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=PS';
$creadpf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&&type=reading&lang=FR&content=PS';
if ( $curl_exists ) {
curl_setopt($ch, CURLOPT_URL, $ctitlepf);
$titlepf = curl_exec($ch);
}
if ( $curl_exists ) {
curl_setopt($ch, CURLOPT_URL, $creadpf);
$readpf = curl_exec($ch);
}
?>
<div id="readp" class="tabbertab">
<h2>Psaume</h2>
<div id='titlepf' class='rtitle'>
<?php echo $titlepf; ?>
</div>
<div id='readpf' class='rtext'>
<?php echo $readpf; ?>
</div>
</div>
<?php
$titlesf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=SR';
$readsf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading&lang=FR&content=SR';
if ( $curl_exists ) {
curl_setopt($ch, CURLOPT_URL, $titlesf);
$titlesf = curl_exec($ch);
}
if ( $curl_exists ) {
curl_setopt($ch, CURLOPT_URL, $readsf);
$readsf = curl_exec($ch);
}
?>
<div id="reads" class="tabbertab">
<h2>DeuxiËme Lecture</h2>
<div id='titlesf' class='rtitle'>
<?php echo $titlesf; ?>
</div>
<div id='readsf' class='rtext'>
<?php echo $readsf; ?>
</div>
</div>
<?php
$titlegf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading_lt&lang=FR&content=GSP';
$readgf = 'http://feed.evangelizo.org/reader.php?date='.$todayfeast.'&type=reading&lang=FR&content=GSP';
if ( $curl_exists ) {
curl_setopt($ch, CURLOPT_URL, $titlegf);
$titlegf = curl_exec($ch);
}
if ( $curl_exists ) {
curl_setopt($ch, CURLOPT_URL, $readgf);
$readgf = curl_exec($ch);
}
?>
<div id="readg" class="tabbertab">
<h2>Evangile</h2>
<div id='titlegf' class='rtitle'>
<?php echo $titlegf; ?>
</div>
<div id='readgf' class='rtext'>
<?php echo $readgf; ?>
</div>
</div>
<?php
// Remember to always close the session and free all resources
if ( $curl_exists ) {
curl_close($ch);
}
?>
</div>
</div>
答案 0 :(得分:1)
我不能确切地说出它为什么会发生,但是看到你从同一台服务器取出,我不会每次都创建一个新的curl句柄。只需使用相同的,只需更改CURLOPT_URL即可。这样,服务器将重用TCP连接,而不是每次都创建一个新连接:
$ch = get_curl();
curl_setopt($ch, CURLOPT_URL, $ctitleff);
$titleff = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, $creadff);
$readff = curl_exec($ch);
...
curl_close($ch);
不确定这是一个答案,但是要尝试:)
另外,您可能需要查看curl-multi-exec()