PHP获取新浪天气数据

第一次在博客园发文章。没有内容,直接上代码;如下:

1 <?php
2 /**
3 * 获取新浪天气数据
4 *
5 * Project: Product Library Management System

7 *
8 * Site: http://www.cnblogs.com/wgw8299
9 *
10 * $Id: index.php 241 2009-11-30 00:14:01 wgw8299 $
11 *
12 * Copyright (C) 2008-2009 Tiwer All Rights Reserved.
13 */
14
15
16 /**
17 * 获取URL地址内容
18 *
19 * @param string $url 地址
20 *
21 * @return mixed
22 */
23 function getUrlContent($url) {
24
25 $url_parsed = parse_url($url);
26 $host = $url_parsed['host'];
27 $port = $url_parsed['port'];
28
29 /* Port */
30 if ( $port == 0 ) {
31 $port = 80;
32 }
33
34 /* Path */
35 $path = $url_parsed['path'];
36 if (empty($path)) {
37 $path = "/";
38 }
39
40 /* query */
41 if ( $url_parsed['query'] != "" ) {
42 $path .= "?".$url_parsed['query'];
43 }
44
45 /* Open Page Content */
46 $out = "GET {$path} HTTP/1.0\r\nHost: {$host}\r\n\r\n";
47 if ($fp = @fsockopen( $host, $port, $errno, $errstr, 30 )) {
48 fwrite($fp,$out);
49 $body = false;
50 while (!feof($fp)) {
51 $s = fgets($fp,1024);
52 if ($body) {
53 $in .= $s;
54 }
55 if ( $s == "\r\n" ) {
56 $body = true;
57 }
58 }
59 fclose($fp);
60 return $in;
61 } else {
62 return false;
63 }
64 }
65
66 /* 生成JS代码 */
67 $conttent = getUrlContent("http://php.weather.sina.com.cn/search.php?city=独山&c=1&dpc=1");
68 preg_match_all('/javascript:sent_to_vb(.*)i6/isU', $conttent, $match_img);
69
70 $var = explode("'" , $match_img[1][0]);
71 echo "document.write('". $var[3] . "');\n";
72  ?>
73
74  



原创作者:Tiwer

文章出处:http://wgw8299.cnblogs.com/

关于作者:专注于互联网技术研究与开发、企业信息化解决方案。现主要从事PHP, WinForm、ASP.NET、JavaScript、UI、CSS、Linux/Uinx、C++,Google Android等方面的项目开发、架构工作。 

版权说明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

posted @ 2010-08-26 17:13  wgw8299  阅读(1579)  评论(0编辑  收藏  举报