API 本地通话有效但现场通话无效
API Call works in local but not in live
我在 PHP 中有一个自定义构建的 API,这是对 MariaDB 数据库的简单 GET 请求,用于获取数据库中的记录列表,returned 在 JSON.
文件夹结构为
/api
/api/some/read.php
/api/some/read2.php
/config
/config/Database.php
/models
/models/call.php
/models/call2.php
read.php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
include_once '../../config/Database.php';
include_once '../../models/call.php';
$database = new Database();
$db = $database->connect();
$post = new Call($db);
$result = $post->read();
$num = $result->rowCount();
if ($num > 0) {
$post_arr = array();
$post_arr['data'] = array();
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
extract($row);
$post_item = array(
'id' => $id,
'title' => $title,
'category' => $category,
'address' => $address,
'website' => $website,
'phoneNumber' => $phoneNumber,
'placeUrl' => $placeUrl,
'info' => $info,
);
array_push($post_arr['data'], $post_item);
}
echo json_encode($post_arr);
} else {
echo json_encode(
array('message' => 'No info Found')
);
}
这里是read2.php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
include_once '../../config/Database.php';
include_once '../../models/call2.php';
$database = new Database();
$db = $database->connect();
$post = new Call($db);
$result = $post->read();
$num = $result->rowCount();
if ($num > 0) {
$post_arr = array();
$post_arr['data'] = array();
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
extract($row);
$post_item = array(
'id' => $id,
'title' => $title,
'category' => $category,
'address' => $address,
'website' => $website,
'phoneNumber' => $phoneNumber,
'placeUrl' => $placeUrl,
'info' => $info,
);
array_push($post_arr['data'], $post_item);
}
echo json_encode($post_arr);
} else {
echo json_encode(
array('message' => 'No info Found')
);
}
然后是Database.php
<?php
class Database {
private $host = 'localhost';
private $db_name = 'data';
private $username = '01';
private $password = '01!';
private $conn;
public function connect() {
$this->conn = null;
try {
$this->conn = new PDO('mysql:host=' .$this->host . ';dbname=' .$this->db_name,
$this->username, $this->password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e) {
echo 'Connection Error: ' . $e->getMessage();
}
return $this->conn;
}
}
call.php
<?php
class Call {
private $conn;
private $table = 'tester';
public $id;
public $title;
public $client_name;
public $category;
public $address;
public $website;
public $phoneNumber;
public $placeUrl;
public $info;
public function __construct($db) {
$this->conn = $db;
}
public function read() {
$query = 'SELECT
id,
title,
category,
address,
website,
phoneNumber,
placeUrl,
info
FROM
' .$this->table. '
ORDER BY
id DESC';
$stmt = $this->conn->prepare($query);
$stmt->execute();
return $stmt;
}
}
call2.php
<?php
class Call {
private $conn;
private $table = 'test';
public $id;
public $title;
public $client_name;
public $category;
public $address;
public $website;
public $phoneNumber;
public $placeUrl;
public $info;
public function __construct($db) {
$this->conn = $db;
}
public function read() {
$query = 'SELECT
id,
title,
category,
address,
website,
phoneNumber,
placeUrl,
info
FROM
' .$this->table. '
ORDER BY
id DESC';
$stmt = $this->conn->prepare($query);
$stmt->execute();
return $stmt;
}
}
数据库结构如下
data
-test
-tester
现在是奇怪的部分:
我在本地机器上安装了 Postman 和 Xampp。当我访问 mysite.com/api/some/read2.php 时,我得到以下 json return:
"data": [
{
"id": "115",
"title": "graphers",
"category": "service",
"address": "",
"website": ".com",
"phoneNumber": "+1 number",
"placeUrl": "https://**",
"info": ""
},
{
"id": "114",
"title": "place name",
"category": "categoryname",
"address": "the address",
"website": ".com",
"phoneNumber": "+1 ",
"placeUrl": "https://**",
"info": ""
},
etc.
我认为没有必要 post 完整的 115 条记录 returned,我相信你们明白我在说什么。
当我访问我的网站时。com/api/some/read。php 即使 return 有 384 条记录,我也一无所获。
哦,是的,奇怪的部分:
我将“api”文件夹压缩为 zip,并将“数据”数据库(结构和数据)导出到 .sql
然后我重新安装了 xampp 和 postman 并配置了所有内容,进入 phpMyAdmin 并将 .sql 文件导入到新数据库中。将 'api' 文件夹解压缩到我机器上的本地主机中。
localhost/api/some/read.php returns:
{"data":[{"id":"384","title":
所有 384 条记录....
我只更改了本地计算机 MariaDB 设置的用户名和密码。
我错过了什么吗?显然。
我尝试过的事情:
- 我在 read.php 的末尾添加了以下内容,并在 call.json 文件中得到了我想要的所有 384 条记录的 return。
$req_dump = print_r( $post_arr, true );
$fp = file_put_contents( 'call.json', $req_dump );
我创建了一个全新的数据库并更改了 table 和数据库的名称,并创建了一个具有完全权限的新用户,同样的事情发生了 - 在本地工作,read.php return结果为 200,但没有数据。
正在删除我网站上的所有数据库信息和所有用户(首先备份当然我没那么疯狂,只有 35/48 没什么大不了的。)更新 PHP/Apache/MariaDB 在我的网站上,清除缓存墙,在免费托管系统上创建一个新的不安全网站,使用相同的信息:在每一步之间,我一遍又一遍地创建新的数据库和新的 php 文件以及相同的东西.不管字面上的 Ctrl+X 和 Ctrl+V read2.php 和 call2.php,到 read.php 和 call.php,更改 table 名称,并包含在 read.php 到 models/call.php.. 没有任何作用....如果我将其更改为 call2.php,read.php returns 来自的数据测试,如果我改变
private $table = "test";
至
private $table = "tester";
我没有得到任何数据。 'test' 我得到数据。 table 列名称完全相同,顺序完全相同,命名完全相同,并且每一行都填充了相同类型信息的变体。
我不明白。我解释多了。
找到修复。
在 Database.php 我改变了:
$this->username, $this->password);
至
$this->username, $this->password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
在 $post_arr 的数据中,有些字符与 json_encode 不相容。经过大量调试后,我终于发现它返回了这个错误:
Error: SyntaxError: Unexpected end of JSON inputstatus: -1
Resource settings for Get collection (GET)
-------------------
Resource URL: https://eaxmple.com/api/post/
Relative path: read.php
然后我在阅读中更改了这个。php/read2。php
echo json_encode($post_arr);
至
echo json_encode($post_arr);
$req = json_encode($post_arr);
$fp = file_put_contents("req2.json", $req);
我在 PHP 中有一个自定义构建的 API,这是对 MariaDB 数据库的简单 GET 请求,用于获取数据库中的记录列表,returned 在 JSON.
文件夹结构为
/api
/api/some/read.php
/api/some/read2.php
/config
/config/Database.php
/models
/models/call.php
/models/call2.php
read.php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
include_once '../../config/Database.php';
include_once '../../models/call.php';
$database = new Database();
$db = $database->connect();
$post = new Call($db);
$result = $post->read();
$num = $result->rowCount();
if ($num > 0) {
$post_arr = array();
$post_arr['data'] = array();
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
extract($row);
$post_item = array(
'id' => $id,
'title' => $title,
'category' => $category,
'address' => $address,
'website' => $website,
'phoneNumber' => $phoneNumber,
'placeUrl' => $placeUrl,
'info' => $info,
);
array_push($post_arr['data'], $post_item);
}
echo json_encode($post_arr);
} else {
echo json_encode(
array('message' => 'No info Found')
);
}
这里是read2.php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
include_once '../../config/Database.php';
include_once '../../models/call2.php';
$database = new Database();
$db = $database->connect();
$post = new Call($db);
$result = $post->read();
$num = $result->rowCount();
if ($num > 0) {
$post_arr = array();
$post_arr['data'] = array();
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
extract($row);
$post_item = array(
'id' => $id,
'title' => $title,
'category' => $category,
'address' => $address,
'website' => $website,
'phoneNumber' => $phoneNumber,
'placeUrl' => $placeUrl,
'info' => $info,
);
array_push($post_arr['data'], $post_item);
}
echo json_encode($post_arr);
} else {
echo json_encode(
array('message' => 'No info Found')
);
}
然后是Database.php
<?php
class Database {
private $host = 'localhost';
private $db_name = 'data';
private $username = '01';
private $password = '01!';
private $conn;
public function connect() {
$this->conn = null;
try {
$this->conn = new PDO('mysql:host=' .$this->host . ';dbname=' .$this->db_name,
$this->username, $this->password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e) {
echo 'Connection Error: ' . $e->getMessage();
}
return $this->conn;
}
}
call.php
<?php
class Call {
private $conn;
private $table = 'tester';
public $id;
public $title;
public $client_name;
public $category;
public $address;
public $website;
public $phoneNumber;
public $placeUrl;
public $info;
public function __construct($db) {
$this->conn = $db;
}
public function read() {
$query = 'SELECT
id,
title,
category,
address,
website,
phoneNumber,
placeUrl,
info
FROM
' .$this->table. '
ORDER BY
id DESC';
$stmt = $this->conn->prepare($query);
$stmt->execute();
return $stmt;
}
}
call2.php
<?php
class Call {
private $conn;
private $table = 'test';
public $id;
public $title;
public $client_name;
public $category;
public $address;
public $website;
public $phoneNumber;
public $placeUrl;
public $info;
public function __construct($db) {
$this->conn = $db;
}
public function read() {
$query = 'SELECT
id,
title,
category,
address,
website,
phoneNumber,
placeUrl,
info
FROM
' .$this->table. '
ORDER BY
id DESC';
$stmt = $this->conn->prepare($query);
$stmt->execute();
return $stmt;
}
}
数据库结构如下
data
-test
-tester
现在是奇怪的部分: 我在本地机器上安装了 Postman 和 Xampp。当我访问 mysite.com/api/some/read2.php 时,我得到以下 json return:
"data": [
{
"id": "115",
"title": "graphers",
"category": "service",
"address": "",
"website": ".com",
"phoneNumber": "+1 number",
"placeUrl": "https://**",
"info": ""
},
{
"id": "114",
"title": "place name",
"category": "categoryname",
"address": "the address",
"website": ".com",
"phoneNumber": "+1 ",
"placeUrl": "https://**",
"info": ""
},
etc.
我认为没有必要 post 完整的 115 条记录 returned,我相信你们明白我在说什么。
当我访问我的网站时。com/api/some/read。php 即使 return 有 384 条记录,我也一无所获。 哦,是的,奇怪的部分: 我将“api”文件夹压缩为 zip,并将“数据”数据库(结构和数据)导出到 .sql 然后我重新安装了 xampp 和 postman 并配置了所有内容,进入 phpMyAdmin 并将 .sql 文件导入到新数据库中。将 'api' 文件夹解压缩到我机器上的本地主机中。
localhost/api/some/read.php returns:
{"data":[{"id":"384","title":
所有 384 条记录....
我只更改了本地计算机 MariaDB 设置的用户名和密码。 我错过了什么吗?显然。
我尝试过的事情:
- 我在 read.php 的末尾添加了以下内容,并在 call.json 文件中得到了我想要的所有 384 条记录的 return。
$req_dump = print_r( $post_arr, true );
$fp = file_put_contents( 'call.json', $req_dump );
我创建了一个全新的数据库并更改了 table 和数据库的名称,并创建了一个具有完全权限的新用户,同样的事情发生了 - 在本地工作,read.php return结果为 200,但没有数据。
正在删除我网站上的所有数据库信息和所有用户(首先备份当然我没那么疯狂,只有 35/48 没什么大不了的。)更新 PHP/Apache/MariaDB 在我的网站上,清除缓存墙,在免费托管系统上创建一个新的不安全网站,使用相同的信息:在每一步之间,我一遍又一遍地创建新的数据库和新的 php 文件以及相同的东西.不管字面上的 Ctrl+X 和 Ctrl+V read2.php 和 call2.php,到 read.php 和 call.php,更改 table 名称,并包含在 read.php 到 models/call.php.. 没有任何作用....如果我将其更改为 call2.php,read.php returns 来自的数据测试,如果我改变
private $table = "test";
至
private $table = "tester";
我没有得到任何数据。 'test' 我得到数据。 table 列名称完全相同,顺序完全相同,命名完全相同,并且每一行都填充了相同类型信息的变体。
我不明白。我解释多了。
找到修复。 在 Database.php 我改变了:
$this->username, $this->password);
至
$this->username, $this->password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
在 $post_arr 的数据中,有些字符与 json_encode 不相容。经过大量调试后,我终于发现它返回了这个错误:
Error: SyntaxError: Unexpected end of JSON inputstatus: -1
Resource settings for Get collection (GET)
-------------------
Resource URL: https://eaxmple.com/api/post/
Relative path: read.php
然后我在阅读中更改了这个。php/read2。php
echo json_encode($post_arr);
至
echo json_encode($post_arr);
$req = json_encode($post_arr);
$fp = file_put_contents("req2.json", $req);