开放的编程资料库

当前位置:我爱分享网 > PHP教程 > 正文

使用 PHP、MySQL、XML 和 JSON 创建基本 Web 服务

Web 服务正在接管世界。我将 Twitter 的史诗般崛起归功于简单但丰富的 API 的可用性。为什么不为您自己的站点使用相同的模型?下面介绍如何使用一些 PHP 和 MySQL 创建提供 XML 或 JSON 响应的基本 Web 服务。

PHP/MySQL

/* require the user as the parameter */
if(isset($_GET['user']) && intval($_GET['user'])) {

	/* soak in the passed variable or set our own */
	$number_of_posts = isset($_GET['num']) ? intval($_GET['num']) : 10; //10 is the default
	$format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml'; //xml is the default
	$user_id = intval($_GET['user']); //no default

	/* connect to the db */
	$link = mysql_connect('localhost','username','password') or die('Cannot connect to the DB');
	mysql_select_db('db_name',$link) or die('Cannot select the DB');

	/* grab the posts from the db */
	$query = "SELECT post_title, guid FROM wp_posts WHERE post_author = $user_id AND post_status = 'publish' ORDER BY ID DESC LIMIT $number_of_posts";
	$result = mysql_query($query,$link) or die('Errant query:  '.$query);

	/* create one master array of the records */
	$posts = array();
	if(mysql_num_rows($result)) {
		while($post = mysql_fetch_assoc($result)) {
			$posts[] = array('post'=>$post);
		}
	}

	/* output in necessary format */
	if($format == 'json') {
		header('Content-type: application/json');
		echo json_encode(array('posts'=>$posts));
	}
	else {
		header('Content-type: text/xml');
		echo '<posts>';
		foreach($posts as $index => $post) {
			if(is_array($post)) {
				foreach($post as $key => $value) {
					echo '<',$key,'>';
					if(is_array($value)) {
						foreach($value as $tag => $val) {
							echo '<',$tag,'>',htmlentities($val),'</',$tag,'>';
						}
					}
					echo '</',$key,'>';
				}
			}
		}
		echo '</posts>';
	}

	/* disconnect from the db */
	@mysql_close($link);
}

随着访问您的 Web 服务的人数(希望如此),您需要在尝试连接到数据库之前进行充分的验证以避免注入攻击。一旦我们从数据库中获得了所需的结果,我们就会循环遍历结果以填充我们的返回结果数组。根据所需的响应类型,我们以所需的格式输出正确的标头和内容。

以下面的示例 URL 为例:

http://mydomain.com/web-service.php?user=2&num=10

现在,我们可以查看 URL 的可能结果。

XML 输出

<posts>
	<post>
		<post_title>SSLmatic SSL Certificate Giveaway Winners</post_title>
		<guid>https://davidwalsh.name/?p=2304</guid>
	</post>
	<post>
		<post_title>MooTools FileManager</post_title>
		<guid>https://davidwalsh.name/?p=2288</guid>
	</post>
	<post>
		<post_title>PHPTVDB: Using PHP to Retrieve TV Show Information</post_title>
		<guid>https://davidwalsh.name/?p=2266</guid>
	</post>
	<post>
		<post_title>David Walsh: The Lost MooTools Plugins</post_title>
		<guid>https://davidwalsh.name/?p=2258</guid>
	</post>
	<post>
		<post_title>Create Short URLs Using U.Nu</post_title>
		<guid>https://davidwalsh.name/?p=2218</guid>
	</post>
	<post>
		<post_title>Create Bit.ly Short URLs Using PHP</post_title>
		<guid>https://davidwalsh.name/?p=2194</guid>
	</post>
	<post>
		<post_title>Represent Your Repositories Using the GitHub Badge!</post_title>
		<guid>https://davidwalsh.name/?p=2178</guid>
	</post>
	<post>
		<post_title>ZebraTable</post_title>
		<guid>https://davidwalsh.name/?page_id=2172</guid>
	</post>
	<post>
		<post_title>MooTools Zebra Table Plugin</post_title>
		<guid>https://davidwalsh.name/?p=2168</guid>
	</post>
	<post>
		<post_title>SSLmatic: Quality, Cheap SSL Certificates and Giveaway!</post_title>
		<guid>https://davidwalsh.name/?p=2158</guid>
	</post>
</posts>

以下一个示例 URL 为例:

http://mydomain.com/web-service.php?user=2&num=10&format=json

现在,我们可以查看 URL 的可能结果。

JSON 输出

{"posts":[{"post":{"post_title":"SSLmatic SSL Certificate Giveaway Winners","guid":"http:\/\/davidwalsh.name\/?p=2304"}},{"post":{"post_title":"MooTools FileManager","guid":"http:\/\/davidwalsh.name\/?p=2288"}},{"post":{"post_title":"PHPTVDB: Using PHP to Retrieve TV Show Information","guid":"http:\/\/davidwalsh.name\/?p=2266"}},{"post":{"post_title":"David Walsh: The Lost MooTools Plugins","guid":"http:\/\/davidwalsh.name\/?p=2258"}},{"post":{"post_title":"Create Short URLs Using U.Nu","guid":"http:\/\/davidwalsh.name\/?p=2218"}},{"post":{"post_title":"Create Bit.ly Short URLs Using PHP","guid":"http:\/\/davidwalsh.name\/?p=2194"}},{"post":{"post_title":"Represent Your Repositories Using the GitHub Badge!","guid":"http:\/\/davidwalsh.name\/?p=2178"}},{"post":{"post_title":"ZebraTable","guid":"http:\/\/davidwalsh.name\/?page_id=2172"}},{"post":{"post_title":"MooTools Zebra Table Plugin","guid":"http:\/\/davidwalsh.name\/?p=2168"}},{"post":{"post_title":"SSLmatic: Quality, Cheap SSL Certificates and Giveaway!","guid":"http:\/\/davidwalsh.name\/?p=2158"}}]}

创建基本网络服务非常简单,并鼓励您的用户传播有关您的网站或服务的信息。想要更多流量?希望您的网站在不付出所有努力的情况下发展壮大?创建网络服务!

未经允许不得转载:我爱分享网 » 使用 PHP、MySQL、XML 和 JSON 创建基本 Web 服务

感觉很棒!可以赞赏支持我哟~

赞(0) 打赏