使用PHP消费Web服务

时间:2013-10-10 03:46:48

标签: javascript php json web-services

我是Web服务的新手,我想实现一个。我做了一些研究,我偶然发现了这个网站http://www.9lessons.info/2012/05/create-restful-services-api-in-php.html。这里给出的东西,但我不知道如何作为客户端使用这个Web服务。

假设我有一个应该显示用户的客户端文件。

我该怎么做?

感谢。

1 个答案:

答案 0 :(得分:0)

以下是一个示例JSON Web服务代码段供您学习.... 但你应该有一个基本的知识b4潜入......!

例如,下面的代码位于 submit_user_details.php 中 你需要调用页面例如 *本地主机/应用程序的名字/ web服务/ submit_user_details.php *

<?php 
require_once('../Connections/db_connection.php');
?>
<?php 
error_reporting(E_ERROR);
$json = file_get_contents('php://input');
$obj = json_decode($json); 

$pin=$obj->{'pin'};
$name=$obj->{'name'};
$phone=$obj->{'phone'};
$second_number=$obj->{'second_number'};
$home_no=$obj->{'home_no'};
$email=$obj->{'email'};

//mysql_select_db($db, $db_connection)or die ('->>Error selecting database'.mysql_error()); 

$query = "UPDATE `haji_members` SET `name`= '".$name."',`phone`= '".$phone."',`secondary_phone`= '".$second_number."',`home_number`= '".$home_no."',`email`= '".$email."' WHERE pincode='".$pin."'"; 

$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());  

    $posts = array();

    if($result != 1)
    {
        $post = array("message"=>"0");
        $posts[] = array('post'=>$post);
    }

    else{
        $post = array("message"=>"1");
        $posts[] = array('post'=>$post);
    }


header('Content-type: application/json');
echo json_encode(array('posts'=>$posts));

@mysql_close($db_connection);
?> 

输出看起来像

 {"posts":[{"post":{"tot_count":"0","username":null,"password":null}}]}