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

arif khan
  • 23
  • 7
  • check this [question](http://stackoverflow.com/questions/8423404/php-file-get-contents-does-not-work-on-localhost) -- anyway, what does `var_dump($rs)` gave you? – Bagus Tesa Dec 21 '16 at 06:16

2 Answers2

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": "jems@gmail.com",
    "mobile": "8596558499",
    "address": "Banglor",
    "gender": "male",
    "salary": "0",
    "status": "1",
    "image_name": "1320294973-screenshot.jpg"
}, {
    "id": "99",
    "username": ".sapna",
    "password": "sapna9",
    "email": "sapnapapola15@gmail.com",
    "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

Amit Kumar Sahu
  • 495
  • 6
  • 15
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/131194/discussion-on-answer-by-amit-kumar-sahu-how-to-access-data-from-one-website-to-o). – Brad Larson Dec 21 '16 at 21:25
  • sir i need your help if u r free then say yes or no – arif khan Dec 22 '16 at 06:32
  • 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 – arif khan Dec 23 '16 at 09:11
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

Nishant Nair
  • 1,999
  • 1
  • 13
  • 18