decode json data using URL from another server
up vote
1
down vote
favorite
I fetch data from a MySQL database and encode it to 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);
?>
However, when another script tries to decode it:
<?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'];
?>
I get an error.
php mysqli
add a comment |
up vote
1
down vote
favorite
I fetch data from a MySQL database and encode it to 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);
?>
However, when another script tries to decode it:
<?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'];
?>
I get an error.
php mysqli
1
How are you determining that it failed? Have you tried some debugging? Checkjson_last_error_msg(). If both your PHP scripts are running on the same server, you don't need to usefile_get_contents(), see require/include into variable
– Phil
Nov 8 at 22:56
Ah sorry, you said they were on different servers. Ignore that last point
– Phil
Nov 9 at 2:29
1
Show the error message. Also decode thejsonimmediately after encoding in the first script and see what happens. These first steps would help you and others to find what is the problem and where it it is being generated..
– bcperth
Nov 9 at 2:50
there is no error message but there is no data in the page (empty page)
– Mohamed Atef
Nov 9 at 12:27
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I fetch data from a MySQL database and encode it to 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);
?>
However, when another script tries to decode it:
<?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'];
?>
I get an error.
php mysqli
I fetch data from a MySQL database and encode it to 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);
?>
However, when another script tries to decode it:
<?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'];
?>
I get an error.
php mysqli
php mysqli
edited Nov 9 at 3:30
James
11.5k21430
11.5k21430
asked Nov 8 at 22:39
Mohamed Atef
82
82
1
How are you determining that it failed? Have you tried some debugging? Checkjson_last_error_msg(). If both your PHP scripts are running on the same server, you don't need to usefile_get_contents(), see require/include into variable
– Phil
Nov 8 at 22:56
Ah sorry, you said they were on different servers. Ignore that last point
– Phil
Nov 9 at 2:29
1
Show the error message. Also decode thejsonimmediately after encoding in the first script and see what happens. These first steps would help you and others to find what is the problem and where it it is being generated..
– bcperth
Nov 9 at 2:50
there is no error message but there is no data in the page (empty page)
– Mohamed Atef
Nov 9 at 12:27
add a comment |
1
How are you determining that it failed? Have you tried some debugging? Checkjson_last_error_msg(). If both your PHP scripts are running on the same server, you don't need to usefile_get_contents(), see require/include into variable
– Phil
Nov 8 at 22:56
Ah sorry, you said they were on different servers. Ignore that last point
– Phil
Nov 9 at 2:29
1
Show the error message. Also decode thejsonimmediately after encoding in the first script and see what happens. These first steps would help you and others to find what is the problem and where it it is being generated..
– bcperth
Nov 9 at 2:50
there is no error message but there is no data in the page (empty page)
– Mohamed Atef
Nov 9 at 12:27
1
1
How are you determining that it failed? Have you tried some debugging? Check
json_last_error_msg(). If both your PHP scripts are running on the same server, you don't need to use file_get_contents(), see require/include into variable– Phil
Nov 8 at 22:56
How are you determining that it failed? Have you tried some debugging? Check
json_last_error_msg(). If both your PHP scripts are running on the same server, you don't need to use file_get_contents(), see require/include into variable– Phil
Nov 8 at 22:56
Ah sorry, you said they were on different servers. Ignore that last point
– Phil
Nov 9 at 2:29
Ah sorry, you said they were on different servers. Ignore that last point
– Phil
Nov 9 at 2:29
1
1
Show the error message. Also decode the
json immediately after encoding in the first script and see what happens. These first steps would help you and others to find what is the problem and where it it is being generated..– bcperth
Nov 9 at 2:50
Show the error message. Also decode the
json immediately after encoding in the first script and see what happens. These first steps would help you and others to find what is the problem and where it it is being generated..– bcperth
Nov 9 at 2:50
there is no error message but there is no data in the page (empty page)
– Mohamed Atef
Nov 9 at 12:27
there is no error message but there is no data in the page (empty page)
– Mohamed Atef
Nov 9 at 12:27
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217224%2fdecode-json-data-using-url-from-another-server%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
How are you determining that it failed? Have you tried some debugging? Check
json_last_error_msg(). If both your PHP scripts are running on the same server, you don't need to usefile_get_contents(), see require/include into variable– Phil
Nov 8 at 22:56
Ah sorry, you said they were on different servers. Ignore that last point
– Phil
Nov 9 at 2:29
1
Show the error message. Also decode the
jsonimmediately after encoding in the first script and see what happens. These first steps would help you and others to find what is the problem and where it it is being generated..– bcperth
Nov 9 at 2:50
there is no error message but there is no data in the page (empty page)
– Mohamed Atef
Nov 9 at 12:27