多くのURLからコンテンツを取得するページがあります。特定の数のURLが取得された後、カールはnullを返します。特定のセッションでカールが取得するリソースには制限がありますか?PHPのリソースに関する制限リソース
各URLを取得する前にcurl_init()
を、コンテンツを取得した後にcurl_close()
を取得します。私はこれがすぐに資源を解放し、限界に達する状況を防ぐだろうと思った。 curl_init()
を何度も呼び出すことに問題がありますか?
ここでは、URLを取得する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);
?>
EDIT
私はアヤックスでこの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>
マルチカールを見ましたか?ダウンロードプロセスをかなり高速化し、問題を解決する可能性があります。 –
['curl_multi_exec()'](http://php.net/manual/en/function.curl-multi-exec.php)を見てください。そうすれば、スクリプトがずっと速くなります。 「これはすぐにリソースを解放すると思った」 - これはPHPの場合はまれである[read this](http://uk.php.net/manual/en/features.gc.php) – DaveRandom
"特定の数のURLが取得された後、カールはヌルを返します。 - これは毎回同じ数字ですか? (つまり、エラーを引き起こすのは同じURLです)。それぞれのカール・リクエストを個別に試して、それらがすべて孤立して働いていることを確認しましたか? – Ing