我有多个PHP文件,每个文件使用import.io服务和curl将值存储到数据库。有没有办法使用多卷曲并将它们合并到一个文件中?因为我为每个文件运行了很多cron作业,我想简化一些事情 - 运行一个执行数据导入的文件,并将每个数据源存储到MySQL中的特定ID。
<?php
function query() {
$url = "https://api.import.io/store/data/path-to-value";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json"
));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array("input" => $input)));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}
$result = query();
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(count($result->results) > 0) {
$length = 6;
foreach ($result->results as $index => $row) {
$sql = "UPDATE Table-name SET Column-name='" .substr($row->row-name, 0, $length). "' WHERE ID=16";
}
} else {
$sql = "UPDATE Table-name SET Column-name='-' WHERE ID=16";
}
if ($conn->query($sql) === TRUE) {
echo "ID16 updated successfully!";
} else {
echo "Error updating ID16: " . $conn->error;
}
$conn->close();
?>
答案 0 :(得分:1)
您有两种方法可以加快处理速度:
curl_multi_init
并行运行请求OR
$ch
),而不是每次发出请求时关闭它。在这种情况下,您可以保存握手,从而加快了许多请求的进程。