2

I have a page where i get whole data from database http://localhost/staff/home.php

if($btn_search =='Serach' && $btn_search !='') { $search_field = isset($_GET['search_field'])?$_GET['search_field']:''; $all_users = $obj->getAllUsers($search_field); }else{ $all_users = $obj->getAllUsers(); $r = json_encode($all_users); echo "<pre>"; print_r($r); die(); } 

here got the data in JSON format. but now i want same data in other website and the path is

http://localhost/staff_json/ 

and the code i have done like this

<?php $rs = file_get_contents('http://localhost/staff/home.php'); $obj = json_decode($rs); echo "<pre>"; print_r($obj); ?> 

the page is successfully run but data is not showing. please help me if someone know this

1
  • check this question -- anyway, what does var_dump($rs) gave you? Commented Dec 21, 2016 at 6:16

2 Answers 2

2

In your http://localhost/staff/home.php in your else part:

else{ $all_users = $obj->getAllUsers(); $r = json_encode($all_users); echo $r; } 

Updated my code as per your JSON:

put below JSON on "http://localhost/staff/home.php" as it is (actually this is your JSON output you are getting from your code)

[{ "id": "94", "username": "jems", "password": "123", "email": "[email protected]", "mobile": "8596558499", "address": "Banglor", "gender": "male", "salary": "0", "status": "1", "image_name": "1320294973-screenshot.jpg" }, { "id": "99", "username": ".sapna", "password": "sapna9", "email": "[email protected]", "mobile": "8826089668", "address": "laxminagar", "gender": "male", "salary": "0", "status": "1", "image_name": "no-image.jpg" }] 

And below is the code your "http://localhost/staff_json/index.php"

<?php $rs = file_get_contents("http://localhost/staff/home.php"); $obj = json_decode($rs); echo "<pre>"; print_r($obj); ?> 

I am getting the desired output

jsonfile

calling[![][2]]3

enter image description here

AS per your code:

Prepare a separate file "userdata.php" place it to same folder where the "home.php" page /staff/userdata.php

<?php include('config/controller.php'); $obj = new Controller(); $all_users = $obj->getAllUsers(); $r = json_encode($all_users); echo $r; ?> 

And below is the code your "http://localhost/staff_json/index.php"

<?php $rs = file_get_contents("http://localhost/staff/userdata.php"); $obj = json_decode($rs); echo "<pre>"; print_r($obj); ?> 

Then you get the desired output:

Below is your "home.php" page PHP script:

<?php include('config/controller.php'); $obj = new Controller(); include('header.php'); $obj->authenticate(); $all_users = ''; $search_field =''; $btn_search = isset($_GET['btn_search'])?$_GET['btn_search']:''; if($btn_search =='Serach' && $btn_search !='') { $search_field = isset($_GET['search_field'])?$_GET['search_field']:''; $all_users = $obj->getAllUsers($search_field); } ?> 

I removed your else part

Try this hope it will work for you

Sign up to request clarification or add additional context in comments.

3 Comments

Comments are not for extended discussion; this conversation has been moved to chat.
sir i need your help if u r free then say yes or no
sir i have a web page in php which i want to share to others vis whats app,facebook linkedin, and on the same time when we press share button the automatically liked my facebook page if it is possible the tell me then i send my page
0

You need to echo you response

if($btn_search =='Serach' && $btn_search !='') { $search_field = isset($_GET['search_field'])?$_GET['search_field']:''; $all_users = $obj->getAllUsers($search_field); }else{ $all_users = $obj->getAllUsers(); $r = json_encode($all_users); echo $r; } 

Now hit the url. AS per your comment you need the data of other project into your current project. So you need and CURL request to fetch the data from other project into your current one.

 $ch = curl_init(); $curlConfig = array( CURLOPT_URL => "http://localhost/staff/home.php", CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, ); curl_setopt_array($ch, $curlConfig); $result = curl_exec($ch); curl_close($ch); $decodeData = json_decode($result); print_r($decodeData); 

Please try out the above code

16 Comments

ya hii nishant i have done this and m also getting json data but i want access same data in other project and link is localhost/staff_json/index.php in that page i just want data and i have done code but data not showing here <?php $rs = file_get_contents('localhost/staff/home.php'); /*echo $rs; die;*/ $obj = json_decode($rs); echo "<pre>"; echo $obj; //print_r($obj); ?>
sir i need yor help
sir i have a page where i want set the pagination can u help me??
sir i actully almost done but problem is that i am not able to implement my code in the desgine part
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.