arif khan
arif khan

Reputation: 23

how to access data from one website to other using api and json

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

Upvotes: 2

Views: 112

Answers (2)

Nishant Nair
Nishant Nair

Reputation: 1997

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

Upvotes: 0

Amit Kumar Sahu
Amit Kumar Sahu

Reputation: 495

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

Upvotes: 2

Related Questions