获取JSON HTML数据

huangapple go评论86阅读模式
英文:

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

&lt;?php 
header(&#39;content-type: application/json; charset=utf-8&#39;);
/*
* Created by Belal Khan
* website: www.simplifiedcoding.net 
* Retrieve Data From MySQL Database in Android
*/
//database constants
define(&#39;DB_HOST&#39;, &#39;localhost&#39;);
define(&#39;DB_USER&#39;, &#39;&#39;);
define(&#39;DB_PASS&#39;, &#39;&#39;);
define(&#39;DB_NAME&#39;, &#39;&#39;);
//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 &quot;Failed to connect to MySQL: &quot; . mysqli_connect_error();
die();
}
function getAttachment($post_title) {
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$stmt = $conn-&gt;prepare(&quot;SELECT guid,post_title  FROM wp_posts  WHERE `post_type`=&#39;attachment&#39;  AND `post_title` = ?&quot;);
$conn -&gt; set_charset(&quot;utf8&quot;);
$stmt-&gt;bind_param(&quot;s&quot;,$post_title);
$stmt-&gt;execute();
$stmt-&gt;store_result();    
$stmt-&gt;bind_result($guid, $post_title); 
$products_attach = array(); 
if($stmt-&gt;num_rows == 1) {
while($stmt-&gt;fetch()){
$temp = array();
$temp[&#39;guid&#39;] = $guid; 
array_push($products_attach, $temp);
}
return	 	$temp[&#39;guid&#39;];
}
}
//creating a query
$stmt = $conn-&gt;prepare(&quot;SELECT ID, post_title, post_content  FROM wp_posts  WHERE `post_type`=&#39;lsvr_document&#39;AND `post_status`= &#39;publish&#39;;&quot;);
$conn -&gt; set_charset(&quot;utf8&quot;);
//executing the query 
$stmt-&gt;execute();
//binding results to the query 
$stmt-&gt;bind_result($id, $title, $content);
$products = array(); 
//traversing through all the result 
while($stmt-&gt;fetch()){
$temp = array();
$temp[&#39;id&#39;] = $id; 
$temp[&#39;title&#39;] = $title; 
$temp[&#39;link&#39;] = getAttachment($temp[&#39;title&#39;]);
$temp[&#39;content&#39;] = $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);
?&gt;

Some of the Output

       &quot;id&quot;: 867,
&quot;title&quot;: &quot;إفادة إنجاز بناء مخالف&quot;,
&quot;link&quot;: &quot;http:\/\/syncsecser.com\/municipal\/wp-content\/uploads\/2020\/09\/إفادة-إنجاز-بناء-مخالف.pdf&quot;,
&quot;content&quot;: &quot;\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&quot;
},

答案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.

huangapple
  • 本文由 发表于 2020年10月24日 20:03:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64513126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定