如何使用api和json从一个网站访问数据到另一个网站

时间:2016-12-21 06:11:03

标签: php json

我有一个页面,我从数据库中获取整个数据     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();
 }

这里得到了JSON格式的数据。 但现在我想在其他网站上获得相同的数据,路径是

http://localhost/staff_json/ 

和我这样做的代码

<?php
$rs = file_get_contents('http://localhost/staff/home.php');

$obj = json_decode($rs);
echo "<pre>";
print_r($obj);
 ?>

页面已成功运行但数据未显示。 如果有人知道,请帮帮我

2 个答案:

答案 0 :(得分:2)

在你的其他部分http://localhost/staff/home.php中:

<form name="myForm" id="search" action="search.php?searching=true" method="post" >
    <table cellspacing="2px" cellpadding="2px"><br/><br/>   
        <tr>
            <td><?php if(!empty($row1)){ ?>
                <input type="hidden" name="id" value="<?php echo (!empty($row1['id']))?$row1['id']:""?>">
                <?php }else{ ?>
                <input type="hidden" name="save" value='1'>
                <?php } ?>  
                <th style="float:left">First Name:&emsp;&emsp;&emsp;&emsp;&emsp;</th>
            </td>
            <td>
                &emsp;&emsp;&emsp;&emsp;
                <select name="salute" class="select" value="<?php echo (!empty($row1['salute']))?$row1['salute']:""?>">
                    <option>Mr.</option>
                    <option>Ms.</option>
                    <option>Mrs.</option>
                    <option>Dr.</option>
                    <option>Prof.</option>
                </select>
                <input type="text" id="fname" name="fname" class="text"  placeholder="Name is..." onblur="return validName();" onfocus="document.getElementById('fname_error').innerHTML=''"/><br/>
                <label id="fname_error"></label>
            </td>
        </tr>
        <tr>
            <td>
                <th style="float:left">
                    <span><sup>*</sup></span>Last Name:&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
                </th>
            </td>
            <td>
                <input type="text" name="lname" class="text" id="lastname" value="<?php echo (!empty($row1['lname']))?$row1['lname']:""?>" onblur="return validLname();" onfocus="document.getElementById('lastname_error').innerHTML=''"/> <br/>
                <label id="lastname_error"></label>
            </td>
        </tr>
    </table>
    <table align="center" cellspacing="2px" cellpadding="2px"><br/><br/>
        <tr>
            <td>
                <input type="submit" value="Search" name="search" id="search" />&emsp;&emsp;&emsp;&emsp;
            </td>
        </tr>
    </table>
    <table align="center" border="2" bgcolor="white" cellspacing="1px" cellpadding="20px"><br/><br/>
        <tr bgcolor="#4d7fea">
            <th>Id</th>
            <th>Name</th>
            <th>Email</th>
            <th>Age</th>
            <th>Mobile</th>
            <th>Gender</th>
            <th>City</th>
            <th>State</th>
            <th>Country</th>
            <th>Pin</th>
            <th>City</th>
            <th>State</th>
            <th>Country</th>
            <th>Pin</th><th>Actions</th>
        </tr>
        <?php
            if (!empty($_REQUEST['fname'])){
                $params['name']=$_REQUEST['salute']." ".$_REQUEST['fname']." ".$_REQUEST['lname'];
                $sql="SELECT * FROM test WHERE Name LIKE '%".trim($params['name'])."%'";
                $records=$db->mysqli_fetch_assoc($sql);
                while($row=mysqli_fetch_assoc($records)){
        ?>
        <tr>
            <td><?php echo $row['id']; ?></td>
            <td><?php echo $row['name']; ?></td>
            <td><?php echo $row['email']; ?></td>
            <td><?php echo $row['age']; ?></td>
            <td><?php echo $row['mobile']; ?></td>
            <td><?php echo $row['gender']; ?></td>
            <td><?php echo $row['city']; ?></td>
            <td><?php echo $row['state']; ?></td>
            <td><?php echo $row['country']; ?></td>
            <td><?php echo $row['pin']; ?></td>
            <td><?php echo $row['city1']; ?></td>
            <td><?php echo $row['state1']; ?></td>
            <td><?php echo $row['country1']; ?></td>
            <td><?php echo $row['pin1']; ?></td>
            <td><a href="index.php?edit=<?php echo $row['id']; ?>"><span style='color:Purple'><b>Edit</b></span>&emsp;
            <a href="index.php?delete=<?php echo $row['id']; ?>" onclick="return confirm('Are you Sure?')"><span style="color:Red"><b>Delete</b></span>&emsp;</td>
        </tr>
        <?php }  }else{ ?>
        <tr><b>No Data Found</b></tr>
        <?php } ?>
    </table>
    <br/><br/><br/><br/>

根据您的JSON更新了我的代码:

将“http://localhost/staff/home.php”放在JSON下面(实际上这是你从代码中得到的JSON输出)

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

以下是代码“http://localhost/staff_json/index.php

[{
    "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"
}]

我正在获得所需的输出

jsonfile

calling[![][2]] 3

enter image description here

根据您的代码:

准备一个单独的文件“userdata.php”将它放在“home.php”页面/staff/userdata.php

的同一文件夹中
<?php

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

?>

以下是代码“http://localhost/staff_json/index.php

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

?>

然后你得到了所需的输出:

以下是您的“home.php”页面PHP脚本:

<?php

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

?>

我删除了你的其他部分

试试这个希望它适合你

答案 1 :(得分:0)

你需要回应你的回应

 var app = angular.module("myApp", []);
      app.run(function($rootScope) {
         $rootScope.userData = {};
         $rootScope.userData.firstName = "ram";
         $rootScope.userData.lastName = "gopal";
      });
      app.controller("firstController", function($scope, $rootScope) {
      });
      app.controller("secondController", function($scope, $rootScope) {
      });

现在点击了网址。 根据您的评论,您需要将其他项目的数据导入当前项目。因此,您需要和CURL请求将其他项目中的数据提取到当前项目中。

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; 
}

请试用上面的代码