您的位置:首页 > 编程语言 > PHP开发

PHP获取新浪天气数据

2010-08-26 17:13 363 查看
第一次在博客园发文章。没有内容,直接上代码;如下:

<?php
/**
* 获取新浪天气数据
*
* Project: Product Library Management System

*
* Site: http://www.cnblogs.com/wgw8299
*
* $Id: index.php 241 2009-11-30 00:14:01 wgw8299 $
*
* Copyright (C) 2008-2009 Tiwer All Rights Reserved.
*/

/**
* 获取URL地址内容
*
* @param string $url 地址
*
* @return mixed
*/
function getUrlContent($url) {

$url_parsed = parse_url($url);
$host = $url_parsed['host'];
$port = $url_parsed['port'];

/* Port */
if ( $port == 0 ) {
$port = 80;
}

/* Path */
$path = $url_parsed['path'];
if (empty($path)) {
$path = "/";
}

/* query */
if ( $url_parsed['query'] != "" ) {
$path .= "?".$url_parsed['query'];
}

/* Open Page Content */
$out = "GET {$path} HTTP/1.0\r\nHost: {$host}\r\n\r\n";
if ($fp = @fsockopen( $host, $port, $errno, $errstr, 30 )) {
fwrite($fp,$out);
$body = false;
while (!feof($fp)) {
$s = fgets($fp,1024);
if ($body) {
$in .= $s;
}
if ( $s == "\r\n" ) {
$body = true;
}
}
fclose($fp);
return $in;
} else {
return false;
}
}

/*  生成JS代码  */
$conttent = getUrlContent("http://php.weather.sina.com.cn/search.php?city=独山&c=1&dpc=1");
preg_match_all('/javascript:sent_to_vb(.*)i6/isU', $conttent, $match_img);

$var =  explode("'" , $match_img[1][0]);
echo "document.write('". $var[3] . "');\n";
?>


原创作者:Tiwer

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

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

版权说明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: