在后台从 PHP 循环调用 PHP 文件
Calling a PHP file from a PHP loop in background
我有一个 PHP 循环,我需要在后台调用另一个 PHP 文件以 insert/update 一些基于发送给它的变量的信息。我曾尝试使用 CURL,但它似乎不起作用。
我需要它来调用 SQLupdate.php?symbol=$symbol - 是否有另一种方法可以在后台使用参数调用 PHP - 最终是否可以与响应同步完成返回每个循环?
while(($row=mysqli_fetch_array($res)) and ($counter < $max))
{
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "SQLinsert.php",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'symbol' => $symbol,
)
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
}
我要在这里权衡一下,希望得到这个 "away & done"。
尽管从您的 post 中还不完全清楚,您似乎正试图通过 HTTP(s) 协议调用 PHP 文件。
在 PHP 的许多配置中,您可以这样做并通过使用 file_get_contents() 来避免一些潜在的 cURL 开销:
while(($row=mysqli_fetch_array($res)) and ($counter < $max)) {
$postdata = http_build_query(
array(
'symbol' => $row['symbol']
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://example.com/SQLinsert.php', false, $context);
$counter++; // you didn't mention this, but you don't want a everloop...
}
实际上,这几乎是从手册中复制的教科书示例。
改为使用 cURL,就像您最初尝试做的那样,事实上,在循环内调用 curl_setopt() 看起来很干净:
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "http://example.com/SQLinsert.php",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true
);
curl_setopt_array($ch, $curlConfig);
while(($row=mysqli_fetch_array($res)) and ($counter < $max)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, array('symbol' => $row['symbol']));
$result = curl_exec($ch);
$counter++; //see above
}
// do this *after* the loop
curl_close($ch);
现在原来的实际问题可能是$symbol没有初始化;至少,它不在您提供的示例中。我试图通过在我的两个示例中使用 $row['symbol'] 来解决此问题。如果这不是数据库中列的名称,那么您显然需要使用正确的名称。
最后,请注意,通过最快的可用机制访问辅助资源几乎总是更好;如果 "SQLinsert.php" 对于调用脚本是本地的,使用 HTTP(s) 的性能会非常低下,您应该重写系统的两个部分以从本地(例如 'disk-based')点开始工作-观点(已被众多评论者推荐):
//SQLinsert.php
function myInsert($symbol) {
// you've not given us any DB schema information ...
global $db; //hack, *cough*
$sql = "insert into `myTable` (symbol) values('$symbol')";
$res = $this->db->query($sql);
if ($res) return true;
return false;
}
//script.php
require_once("SQLinsert.php");
while(($row=mysqli_fetch_array($res)) and ($counter < $max)) {
$ins = myInsert($row['symbol']);
if ($ins) { // let's only count *good* inserts, which is possible
// because we've written 'myInsert' to return a boolean
$counter++;
}
}
我有一个 PHP 循环,我需要在后台调用另一个 PHP 文件以 insert/update 一些基于发送给它的变量的信息。我曾尝试使用 CURL,但它似乎不起作用。
我需要它来调用 SQLupdate.php?symbol=$symbol - 是否有另一种方法可以在后台使用参数调用 PHP - 最终是否可以与响应同步完成返回每个循环?
while(($row=mysqli_fetch_array($res)) and ($counter < $max))
{
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "SQLinsert.php",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'symbol' => $symbol,
)
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
}
我要在这里权衡一下,希望得到这个 "away & done"。
尽管从您的 post 中还不完全清楚,您似乎正试图通过 HTTP(s) 协议调用 PHP 文件。
在 PHP 的许多配置中,您可以这样做并通过使用 file_get_contents() 来避免一些潜在的 cURL 开销:
while(($row=mysqli_fetch_array($res)) and ($counter < $max)) {
$postdata = http_build_query(
array(
'symbol' => $row['symbol']
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://example.com/SQLinsert.php', false, $context);
$counter++; // you didn't mention this, but you don't want a everloop...
}
实际上,这几乎是从手册中复制的教科书示例。
改为使用 cURL,就像您最初尝试做的那样,事实上,在循环内调用 curl_setopt() 看起来很干净:
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "http://example.com/SQLinsert.php",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true
);
curl_setopt_array($ch, $curlConfig);
while(($row=mysqli_fetch_array($res)) and ($counter < $max)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, array('symbol' => $row['symbol']));
$result = curl_exec($ch);
$counter++; //see above
}
// do this *after* the loop
curl_close($ch);
现在原来的实际问题可能是$symbol没有初始化;至少,它不在您提供的示例中。我试图通过在我的两个示例中使用 $row['symbol'] 来解决此问题。如果这不是数据库中列的名称,那么您显然需要使用正确的名称。
最后,请注意,通过最快的可用机制访问辅助资源几乎总是更好;如果 "SQLinsert.php" 对于调用脚本是本地的,使用 HTTP(s) 的性能会非常低下,您应该重写系统的两个部分以从本地(例如 'disk-based')点开始工作-观点(已被众多评论者推荐):
//SQLinsert.php
function myInsert($symbol) {
// you've not given us any DB schema information ...
global $db; //hack, *cough*
$sql = "insert into `myTable` (symbol) values('$symbol')";
$res = $this->db->query($sql);
if ($res) return true;
return false;
}
//script.php
require_once("SQLinsert.php");
while(($row=mysqli_fetch_array($res)) and ($counter < $max)) {
$ins = myInsert($row['symbol']);
if ($ins) { // let's only count *good* inserts, which is possible
// because we've written 'myInsert' to return a boolean
$counter++;
}
}