英文:
Getting JSON html data
问题
<?php
header('content-type: application/json; charset=utf-8');
/*
* Created by Belal Khan
* website: www.simplifiedcoding.net
* Retrieve Data From MySQL Database in Android
*/
//database constants
define('DB_HOST', 'localhost');
define('DB_USER', '');
define('DB_PASS', '');
define('DB_NAME', '');
//connecting to database and getting the connection object
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
//Checking if any error occured while connecting
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
function getAttachment($post_title) {
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$stmt = $conn->prepare("SELECT guid,post_title FROM wp_posts WHERE `post_type`='attachment' AND `post_title` = ?");
$conn -> set_charset("utf8");
$stmt->bind_param("s", $post_title);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($guid, $post_title);
$products_attach = array();
if($stmt->num_rows == 1) {
while($stmt->fetch()){
$temp = array();
$temp['guid'] = $guid;
array_push($products_attach, $temp);
}
return $temp['guid'];
}
}
//creating a query
$stmt = $conn->prepare("SELECT ID, post_title, post_content FROM wp_posts WHERE `post_type`='lsvr_document'AND `post_status`= 'publish';");
$conn -> set_charset("utf8");
//executing the query
$stmt->execute();
//binding results to the query
$stmt->bind_result($id, $title, $content);
$products = array();
//traversing through all the result
while($stmt->fetch()){
$temp = array();
$temp['id'] = $id;
$temp['title'] = $title;
$temp['link'] = getAttachment($temp['title']);
$temp['content'] = $content;
array_push($products, $temp);
}
//displaying the result in json format
// echo json_encode($products, JSON_UNESCAPED_UNICODE | JSON_HEX_TAG );
$json = json_encode($products, JSON_HEX_TAG | JSON_PRETTY_PRINT | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
echo $json;
//print_r($products);
?>
英文:
I am getting JSON data from a word press database to add them to a mobile app using android studio. However, am facing many problems.
First, I need to keep the post content correctly viewed, but what about the html tags ?
After searching I added JSON_HEX_TAG | JSON_PRETTY_PRINT | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE to my output, but that didn't solve my problem. =
PHP CODE
<?php
header('content-type: application/json; charset=utf-8');
/*
* Created by Belal Khan
* website: www.simplifiedcoding.net
* Retrieve Data From MySQL Database in Android
*/
//database constants
define('DB_HOST', 'localhost');
define('DB_USER', '');
define('DB_PASS', '');
define('DB_NAME', '');
//connecting to database and getting the connection object
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
//Checking if any error occured while connecting
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
function getAttachment($post_title) {
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$stmt = $conn->prepare("SELECT guid,post_title FROM wp_posts WHERE `post_type`='attachment' AND `post_title` = ?");
$conn -> set_charset("utf8");
$stmt->bind_param("s",$post_title);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($guid, $post_title);
$products_attach = array();
if($stmt->num_rows == 1) {
while($stmt->fetch()){
$temp = array();
$temp['guid'] = $guid;
array_push($products_attach, $temp);
}
return $temp['guid'];
}
}
//creating a query
$stmt = $conn->prepare("SELECT ID, post_title, post_content FROM wp_posts WHERE `post_type`='lsvr_document'AND `post_status`= 'publish';");
$conn -> set_charset("utf8");
//executing the query
$stmt->execute();
//binding results to the query
$stmt->bind_result($id, $title, $content);
$products = array();
//traversing through all the result
while($stmt->fetch()){
$temp = array();
$temp['id'] = $id;
$temp['title'] = $title;
$temp['link'] = getAttachment($temp['title']);
$temp['content'] = $content;
array_push($products, $temp);
}
//displaying the result in json format
// echo json_encode($products, JSON_UNESCAPED_UNICODE | JSON_HEX_TAG );
$json = json_encode($products, JSON_HEX_TAG | JSON_PRETTY_PRINT | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
echo $json;
//print_r($products);
?>
Some of the Output
"id": 867,
"title": "إفادة إنجاز بناء مخالف",
"link": "http:\/\/syncsecser.com\/municipal\/wp-content\/uploads\/2020\/09\/إفادة-إنجاز-بناء-مخالف.pdf",
"content": "\u003C!-- wp:heading --\u003E\n\u003Ch2\u003Eالمستندات المرفقة و أماكن إنجازها\u003C\/h2\u003E\n\u003C!-- \/wp:heading --\u003E\n\n\u003C!-- wp:paragraph --\u003E\n\u003Cp\u003E1. طلب موقع من صاحب العلاقة أو من ينوب عنه قانوناً.\u003Cbr\u003E2. إفادة عقارية\u0026nbsp;شاملة.\u003Cbr\u003E3. أي مستند يثبت تاريخ إنجاز المخالفة.\u003C\/p\u003E\n\u003C!-- \/wp:paragraph --\u003E\n\n\u003C!-- wp:table {\u0022className\u0022:\u0022is-style-regular\u0022} --\u003E\n\u003Cfigure id=\u0022table1\u0022 class=\u0022wp-block-table is-style-regular\u0022\u003E\u003Ctable\u003E\u003Ctbody\u003E\u003Ctr\u003E\u003Ctd\u003E\u003Cbr\u003E\u003Cbr\u003E-\u003Cstrong\u003E\u003Cu\u003Eمدّة\u0026nbsp;الإنجاز :\u003C\/u\u003E\u003C\/strong\u003E\u003Cbr\u003E\u0026nbsp;ثلاثة أيام سنداً\u0026nbsp;لتعميم وزير الداخلية والشؤون البلدية والقروية رقم 8\/99.\u003Cbr\u003E\u003Cbr\u003E-\u003Cstrong\u003E\u0026nbsp;الرسوم\u0026nbsp;المتوجبة\u003Cbr\u003E\u003Cbr\u003E\u0026nbsp;\u003C\/strong\u003E\u003Cbr\u003E1.\u0026nbsp;\u0026nbsp;رسم الإفادة الفنية:\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 2000 ل.ل.\u003Cbr\u003E\u0026nbsp;\u003Cbr\u003E2.\u0026nbsp;\u0026nbsp;رسم طابع مالي:\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;1000 ل.ل.\u003Cbr\u003E\u003C\/td\u003E\u003C\/tr\u003E\u003C\/tbody\u003E\u003C\/table\u003E\u003C\/figure\u003E\n\u003C!-- \/wp:table --\u003E"
},
答案1
得分: 0
一个简单的解决方案是,你可以将你的内容编码为base64格式,然后在你的移动应用程序或任何其他需要解析内容的页面中,你只需简单地对这个base64进行解码。
英文:
A simple solution is you can encode your content to base64 and then in your mobile app or any other page you want to parse the content, you can just simply decode this base64.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论