JSON PHP


فهرست مطالب

    نمایش فهرست مطالب


.ﺖﺳﺍ ﺭﻭﺮﺳ ﺏﻭ ﮏﯾ ﺯﺍ ﺎﻫ ﻩﺩﺍﺩ ﻥﺪﻧﺍﻮﺧ JSON ﺞﯾﺍﺭ ﯼﺎﻫﺩﺮﺑﺭﺎﮐ ﺯﺍ ﯽﮑﯾ

ﺪﯿﻨﮐ ﻪﻟﺩﺎﺒﻣ ﺮﮕﯾﺪﮑﯾ ﻦﯿﺑ ﺍﺭ JSON ﯼﺎﻫ ﻩﺩﺍﺩ ﻪﻧﻮﮕﭼ ﻪﮐ ﺪﻫﺩ ﯽﻣ ﺩﺎﯾ ﺎﻤﺷ ﻪﺑ ﻞﺼﻓ ﻦﯾﺍ


PHP ﻞﯾﺎﻓ

PHP ﺖﯾﺮﯾﺪﻣ ﯼﺍﺮﺑ ﯽﻠﺧﺍﺩ ﻊﺑﺍﻮﺗ ﯽﺧﺮﺑ ﯼﺍﺭﺍﺩ JSON ﺖﺳﺍ.

ﺩﺮﮐ ﻞﯾﺪﺒﺗ JSON ﻪﺑ PHP ﻊﺑﺎﺗ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ ﻥﺍﻮﺗ ﯽﻣ ﺍﺭ PHP ﺭﺩ ءﺎﯿﺷﺍ

PHP ﻞﯾﺎﻓ

 <?php
$myObj->name = "John";
$myObj->age = 30;
$myObj->city = "New 
  York";
$myJSON = json_encode($myObj);
echo $myJSON;
?>

ﯼﺮﺘﺸﻣ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ

ﺪﻨﮐ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ PHP ﺖﺳﺍﻮﺧﺭﺩ ﯼﺍﺮﺑ AJAX ﯽﻧﺍﻮﺧﺍﺮﻓ ﮏﯾ ﺯﺍ ﻪﮐ ﺖﺳﺍ ﺖﻨﯾﻼﮐ ﯼﻭﺭ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﮏﯾ

ﻝﺎﺜﻣ

:ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﺷ ﮏﯾ ﻪﺑ ﻪﺠﯿﺘﻧ ﻞﯾﺪﺒﺗ ﯼﺍﺮﺑ()JSON.parse ﺯﺍ

const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
  const myObj = JSON.parse(this.responseText);
  document.getElementById("demo").innerHTML = myObj.name;
}
xmlhttp.open("GET", "demo_file.php");
xmlhttp.send();

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Get JSON Data from a PHP Server</h2>
<p id="demo"></p>

<script>
const xmlhttp = new XMLHttpRequest();

xmlhttp.onload = function() {
  const myObj = JSON.parse(this.responseText);
  document.getElementById("demo").innerHTML = myObj.name;
}
xmlhttp.open("GET", "demo_file.php");
xmlhttp.send();
</script>

</body>
</html>


ﯽﭘ ﭺﺍ ﯽﭘ ﻪﯾﺍﺭﺁ

ﺪﻧﻮﺷ ﯽﻣ ﻞﯾﺪﺒﺗ JSON ﻪﺑ ﺰﯿﻧ PHP ﺭﺩ ﺎﻫ ﻪﯾﺍﺭﺁ ،PHP ﻊﺑﺎﺗ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﻡﺎﮕﻨﻫ

PHP ﻞﯾﺎﻓ

 <?php
$myArr = array("John", "Mary", "Peter", "Sally");
$myJSON = json_encode($myArr);
echo $myJSON;
?>

ﯼﺮﺘﺸﻣ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ

ﺪﻨﮐ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ PHP ﺖﺳﺍﻮﺧﺭﺩ ﯼﺍﺮﺑ AJAX ﯽﻧﺍﻮﺧﺍﺮﻓ ﮏﯾ ﺯﺍ ﻪﮐ ﺖﺳﺍ ﺖﻨﯾﻼﮐ ﯼﻭﺭ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﮏﯾ

ﻝﺎﺜﻣ

:ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻪﯾﺍﺭﺁ ﻪﺑ ﻪﺠﯿﺘﻧ ﻞﯾﺪﺒﺗ ﯼﺍﺮﺑ()JSON.parse ﺯﺍ

var xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
  const myObj = JSON.parse(this.responseText);
  document.getElementById("demo").innerHTML = myObj[2];
}
xmlhttp.open("GET", "demo_file_array.php", true);
xmlhttp.send();

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Get JSON Data from a PHP Server</h2>
<p>Convert the data into a JavaScript array:</p>
<p id="demo"></p>

<script>
const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
  const myObj = JSON.parse(this.responseText);
  document.getElementById("demo").innerHTML = myObj[2];
}
xmlhttp.open("GET", "demo_file_array.php");
xmlhttp.send();
</script>

</body>
</html>

PHP ﻩﺩﺍﺩ ﻩﺎﮕﯾﺎﭘ

PHP ﺩﺮﮐ ﻩﺩﺎﻔﺘﺳﺍ ﻩﺩﺍﺩ ﻩﺎﮕﯾﺎﭘ ﻪﺑ ﯽﺳﺮﺘﺳﺩ ﯼﺍﺮﺑ ﻥﺁ ﺯﺍ ﻥﺍﻮﺗ ﯽﻣ ﻭ ﺖﺳﺍ ﺭﻭﺮﺳ ﺖﻤﺳ ﯽﺴﯾﻮﻧ ﻪﻣﺎﻧﺮﺑ

ﺪﯿﻨﮐ ﻝﺎﺳﺭﺍ ﻥﺁ ﻪﺑ ﺍﺭ ﯽﺘﺳﺍﻮﺧﺭﺩ ﺪﯿﻫﺍﻮﺧ ﯽﻣ ﻭ ﺪﯾﺭﺍﺩ ﺩﻮﺧ ﺭﻭﺮﺳ ﯼﻭﺭ ﻩﺩﺍﺩ ﻩﺎﮕﯾﺎﭘ ﮏﯾ ﺪﯿﻨﮐ ﺭﻮﺼﺗ

.ﺪﻨﮐ ﻒﯿﺻﻮﺗ ،ﺪﯿﻧﺍﺩﺮﮔﺮﺑ ﺪﯿﻫﺍﻮﺧ ﯽﻣ ﻪﮐ ﺍﺭ ﯽﯾﺎﻫ ﻒﯾﺩﺭ ﺩﺍﺪﻌﺗ ﻪﮐ ﺪﯾﺯﺎﺴﺑ JSON ﯽﺷ ﮏﯾ ،ﺖﻨﯾﻼﮐ ﺭﺩ

ﺪﯿﻨﮐ ﻞﯾﺪﺒﺗ a ﻪﺑ ﺍﺭ JSON ﯽﺷ ،ﺭﻭﺮﺳ ﻪﺑ ﺖﺳﺍﻮﺧﺭﺩ ﻝﺎﺳﺭﺍ ﺯﺍ ﻞﺒﻗ

ﻝﺎﺜﻣ

:ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ JSON ﻪﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﺷ ﻞﯾﺪﺒﺗ ﯼﺍﺮﺑ()JSON.stringify ﺯﺍ

const limit = {"limit":10};
const dbParam = JSON.stringify(limit);
xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
  document.getElementById("demo").innerHTML = this.responseText;
}
xmlhttp.open("GET","json_demo_db.php?x=" + dbParam);
xmlhttp.send();

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Get JSON Data from a PHP Server</h2>
<p>The JSON received from the PHP file:</p>

<p id="demo"></p>

<script>
const dbParam = JSON.stringify({"limit":10});

const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
  document.getElementById("demo").innerHTML = this.responseText;
}
xmlhttp.open("GET", "json_demo_db.php?x=" + dbParam);
xmlhttp.send();
</script>

</body>
</html>

:ﻩﺪﺷ ﻩﺩﺍﺩ ﺢﯿﺿﻮﺗ ﻝﺎﺜﻣ

  • .ﺪﯿﻨﮐ ﻒﯾﺮﻌﺗ ﺍﺭ "Limit" ﺭﺍﺪﻘﻣ ﻭ ﯽﮔﮋﯾﻭ ﮏﯾ ﯼﻭﺎﺣ ﯽﺷ ﮏﯾ

  • .ﺪﯿﻨﮐ ﻞﯾﺪﺒﺗ JSON ﻪﺘﺷﺭ ﮏﯾ ﻪﺑ ﺍﺭ ﯽﺷ


  • .ﺪﯿﻨﮐ ﻝﺎﺳﺭﺍ ﺮﺘﻣﺍﺭﺎﭘ ﻥﺍﻮﻨﻋ ﻪﺑ JSON ﻪﺘﺷﺭ ﺎﺑ PHP ﻞﯾﺎﻓ ﻪﺑ ﺖﺳﺍﻮﺧﺭﺩ ﮏﯾ

  • (JSON ﻥﺍﻮﻨﻋ ﻪﺑ) ﺩﺩﺮﮔﺮﺑ ﻪﺠﯿﺘﻧ ﺎﺑ ﺖﺳﺍﻮﺧﺭﺩ ﺎﺗ ﺪﯿﻨﮐ ﺮﺒﺻ

  • .PHP ﻞﯾﺎﻓ ﺯﺍ ﯽﺘﻓﺎﯾﺭﺩ ﻪﺠﯿﺘﻧ ﺶﯾﺎﻤﻧ

:ﺪﯾﺯﺍﺪﻨﯿﺑ ﯽﻫﺎﮕﻧ PHP ﻞﯾﺎﻓ ﻪﺑ

PHP ﻞﯾﺎﻓ

 <?php
header("Content-Type: application/json; charset=UTF-8");
$obj = 
  json_decode($_GET["x"], false);
  
$conn = new mysqli("myServer", "myUser", "myPassword", "Northwind");
  $stmt = $conn->prepare("SELECT name FROM customers LIMIT ?");
$stmt->bind_param("s", $obj->limit);
$stmt->execute();
$result = $stmt->get_result();
  $outp = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($outp);
?>

:ﺩﺍﺩ ﺢﯿﺿﻮﺗ PHP ﻞﯾﺎﻓ

  • ﺪﯿﻨﮐ ﻞﯾﺪﺒﺗ ﯽﺷ ﮏﯾ ﻪﺑ ﺍﺭ ﺖﺳﺍﻮﺧﺭﺩ PHP ﻊﺑﺎﺗ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ

  • .ﺪﯿﻨﮐ ﺮﭘ ﯽﺘﺳﺍﻮﺧﺭﺩ ﯼﺎﻫ ﻩﺩﺍﺩ ﺎﺑ ﺍﺭ ﻪﯾﺍﺭﺁ ﮏﯾ ﻭ ﺪﯿﻨﮐ ﺍﺪﯿﭘ ﯽﺳﺮﺘﺳﺩ ﻩﺩﺍﺩ ﻩﺎﮕﯾﺎﭘ ﻪﺑ

  • ﺪﯿﻧﺍﺩﺮﮔﺮﺑ JSON ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ ﺍﺭ ﯽﺷ ﻭ ﺪﯿﻨﮐ ﻪﻓﺎﺿﺍ ﯽﺷ ﮏﯾ ﻪﺑ ﺍﺭ ﻪﯾﺍﺭﺁ


ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﺎﻫ ﻩﺩﺍﺩ ﺯﺍ

ﻝﺎﺜﻣ

xmlhttp.onload = function() {
  const myObj = JSON.parse(this.responseText);
  let text = "";
  for (let x in myObj) {
    text += myObj[x].name + "<br>";
  }
 
  document.getElementById("demo").innerHTML = text;
}

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Get JSON Data from a PHP Server</h2>
<p id="demo"></p>

<script>
const obj = { "limit":10 };
const dbParam = JSON.stringify(obj);
const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
  myObj = JSON.parse(this.responseText);
  let text = ""
  for (let x in myObj) {
    text += myObj[x].name + "<br>";
  }
  document.getElementById("demo").innerHTML = text;
};
xmlhttp.open("GET", "json_demo_db.php?x=" + dbParam);
xmlhttp.send();
</script>

<p>Try changing the "limit" property from 10 to 5.</p>

</body>
</html>

PHP=POST ﺵﻭﺭ

.ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ code class="w3-codespan">POST HTTP> ﺵﻭﺭ ﺯﺍ ﺖﺳﺍ ﺮﺘﻬﺑ ﺐﻠﻏﺍ ،ﺭﻭﺮﺳ ﻪ

.ﺪﯿﻨﮐ ﺺﺨﺸﻣ ﺍﺭ ﺢﯿﺤﺻ ﺭﺪﻫ ﻭ ﺵﻭﺭ ،POST ﺵﻭﺭ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ

:ﺪﺷﺎﺑ send() ﺵﻭﺭ ﻥﺎﻣﻮﮔﺭﺁ ﺪﯾﺎﺑ ﻥﻮﻨﮐﺍ ﺭﻭﺮﺳ ﻪﺑ ﻩﺪﺷ ﻝﺎﺳﺭ

ﻝﺎﺜﻣ

const dbParam = JSON.stringify({"limit":10});
const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
  const myObj = JSON.parse(this.responseText);
  let text ="";
  for (let x in myObj) {
    text += myObj[x].name + "<br>";
  }
  document.getElementById("demo").innerHTML = text;
}
xmlhttp.open("POST", "json_demo_db_post.php");

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("x=" + dbParam);

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Use HTTP POST to Get JSON Data from a PHP Server</h2>

<p id="demo"></p>

<script>
const dbParam = JSON.stringify({"limit":10});
const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
  myObj = JSON.parse(this.responseText);
  let text = "";
  for (let x in myObj) {
    text += myObj[x].name + "<br>";
  }
  document.getElementById("demo").innerHTML = text;
}
xmlhttp.open("POST", "json_demo_db_post.php");
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("x=" + dbParam);
</script>

<p>Try changing the "limit" property from 10 to 5.</p>

</body>
</html>

.ﺖﺳﺍ ﻪﺘﻓﺎﯾ ﻝﺎﻘﺘﻧﺍ ﯼﺎﻫ ﻩﺩﺍﺩ ﺖﻓﺎﯾﺭﺩ ﺵﻭﺭ PHP ﻞﯾﺎﻓ ﺭﺩ ﺕﻭﺎﻔﺗ ﺎﻬﻨﺗ

PHP ﻞﯾﺎﻓ

:ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ POST_$ﺯﺍ GET_$ﯼﺎﺟ ﻪﺑ

 <?php
header("Content-Type: application/json; charset=UTF-8");
  $obj = 
  json_decode($_POST["x"], false);
  
  
$conn = new mysqli("myServer", "myUser", "myPassword", "Northwind");
  $stmt = $conn->prepare("SELECT name FROM customers LIMIT ?");
$stmt->bind_param("s", 
  $obj->limit);
$stmt->execute();
$result = $stmt->get_result();
  $outp = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($outp);
?>