我有2个文件夹restdemo1和restdemo2。
1有index.php而2有api.php我试图从表中获取数据并显示它,但是使用curl却什么都没有
index.php->
<?php
ini_set("display_errors", "1");
error_reporting(E_ALL);
$curl = curl_init('http://localhost/restdemo2/api.php');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$result = curl_exec($curl);
$errors = curl_error($curl);
var_dump($result);
print_r(json_decode($result));
curl_close($curl);
api.php-&GT;
<?php
ini_set("display_errors", "1");
error_reporting(E_ALL);
$servername = "localhost";
$username = "root";
$password = "root";
$db = "demo";
// Create connection
$conn = new mysqli($servername, $username, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$data = array();
// output data of each row
while($row = $result->fetch_assoc()) {
$data[]=$row;
//echo "id: " . $row["id"]. " - Name: " . $row["user_fullname"]. " Password: " . $row["user_password"]. "<br>";
}
return json_encode($data);
} else {
return "0 results";
}
答案 0 :(得分:0)
这是一种确定问题所在的简单方法 - 首先,尝试使用浏览器访问api.php,看看您是否收到任何回复。如果没有 - 你的问题就在那里。 ,尝试在另一个URL上运行index.php。如果你没有检索任何东西 - 问题就在那里(或者那里)。
无论如何,请尝试使用CURL:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost/restdemo2/api.php');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$pageResponse = curl_exec($ch);
curl_close($ch);