php - 使用来自另一台服务器的 URL 解码 json 数据
问题描述
我从 MySQL 数据库中获取数据并将其编码为 JSON:
<?php
error_reporting(0);
//Connection information to the Server
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "wordpress";
$conn = new mysqli($servername, $username, $password, $dbname);
mysqli_select_db($dbname);
$sql = "SELECT * FROM `wp_posts` ";
$query = $conn->query($sql);
$dataArray=array();
while($row=mysqli_fetch_array($query)){
$temp['post_title'] = $row['post_title'];
$temp['post_date'] = $row['post_date'];
array_push($dataArray, $temp);
}
echo json_encode(array("wp_posts"=>$dataArray),JSON_UNESCAPED_UNICODE);
?>
但是,当另一个脚本尝试对其进行解码时:
<?php
error_reporting(0);
$get_data = file_get_contents("http://localhost:6060/php/api-test/api.php");
$json = json_decode($get_data, true);
$response = json_decode($get_data, true); //because of true, it's in an array
echo 'Online: '. $response['post_title'];
?>
我得到一个错误。
解决方案
推荐阅读
- android - 为什么 Android 上的 Instant Apps 有两个设置?
- excel - 获取顶部单元格所需的 Excel 公式
- java - Spring MVC with Maven,源服务器没有找到目标资源的当前表示
- c# - 是否可以使用 System.Drawing 命名空间识别字符串中不可见的 unicode 字符?
- entity-framework-migrations - 如何借助 Azure 管道将表/数据库更改迁移到测试/阶段环境
- python - 您如何检查并确保相同的输入不会两次存储在字典中?
- python - 使用 Pandas 中的“to_datetime”函数将月份名称转换为十进制
- react-native - useEffect 中的 API 调用函数在 React Native 中创建无限循环
- css - SVG 在溢出的网站屏幕上渲染时被切断
- python - 为什么即使我提到了 epoch 100,模型也会在 epoch 1 之后停止训练而没有任何警告?