我想问一下curl php ...
我想从数据库中获取数据并使用php curl发送到其他PC并将其保存为纯文本。但我的问题是,系统不从数据库发送数据,只是发送我声明的数据名称。我使用fedora平台......其实我是curl php的初学者......
这里是我的代码... send.php
<?php
$servername = "yusufpsm";
$username = "root";
$password = "921217";
$db_name = "squid";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $db_name);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully\n";
///////////////////////////////////////////////////////////////////////
$sql = "SELECT domain_name, domain_content, reason FROM domain";
$domain= $_POST['domain_name'];
$content= $_POST['domain_content'];
$reason = $_POST['reason'];
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, '192.168.100.11/update.php');
curl_setopt($curlHandle, CURLOPT_POST, 1);
curl_setopt($curlHandle, CURLOPT_HEADER, 0);
curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, 'domain_name='.$domain.'domain_content='.$content. 'reason='.$reason);
curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
if (!curl_exec($curlHandle)) {
echo 'An error has occurred: ' . curl_error($curlHandle);
}
else {
echo 'everything was successful';
}
}
}
else
{
echo "0 results";
}
curl_close($curlHandle) ;
mysqli_close($conn);
?>
这里是update.php
<?php
$domain_name = $_POST['domain_name'];
$domain_content = $_POST['domain_content'];
$reason = $_POST['reason'];
chmod("/var/www/html/newfile.txt",0777);
$myfile = fopen("/var/www/html/newfile.txt", "w") or die("Unable to open file!");
$txt = $domain_name;
fwrite($myfile, $txt);
$txt = $domain_content;
fwrite($myfile, $txt);
$txt = $reason;
fwrite($myfile, $txt);
fclose($myfile);
?>
答案 0 :(得分:1)
try following code
<?php
$servername = "yusufpsm";
$username = "root";
$password = "921217";
$db_name = "squid";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $db_name);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully\n";
///////////////////////////////////////////////////////////////////////
$sql = "SELECT domain_name, domain_content, reason FROM domain";
/* $domain= $_POST['domain_name'];
$content= $_POST['domain_content']; // don't know what is this
$reason = $_POST['reason'];*/
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$domain= $row['domain_name'];
$content= $row['domain_content']; // get values row-wise from db
$reason = $row['reason'];
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, '192.168.100.11/update.php');
curl_setopt($curlHandle, CURLOPT_POST, 1);
curl_setopt($curlHandle, CURLOPT_HEADER, 0);
curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, 'domain_name='.$domain.'&domain_content='.$content. '&reason='.$reason);
curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
if (!curl_exec($curlHandle)) {
echo 'An error has occurred: ' . curl_error($curlHandle);
}
else {
echo 'everything was successful';
}
}
}
else
{
echo "0 results";
}
curl_close($curlHandle) ;
mysqli_close($conn);
?>