20 使用网络函数和协议函数

20 使用网络函数和协议函数主要内容:介绍PHP所提供的面向网络的函数,这些函数能够使脚本与Internet进行交互。先看了一个例子:程序清单20-1 lookup.php —— 从NASDAQ获得$symbol列表所给出股票的报价脚本
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Stock Quote From NASDAQ</title>
</head>
<body>
<?php
// choose stock to look at
$symbol = ‘AMZN‘;
echo ‘<h1>Stock quote for‘.$symbol.‘</h1>‘;

$url = ‘http://finance.yahoo.com/d/quotes.csv‘.
    ‘?s=‘ . $symbol . ‘&e=.csv&f=slldltlclohgv‘;
if(!($contents = file_get_contents($url)))
{
    die(‘Failure to open ‘.$url);
}

// extract relevant data
list($symbol, $quote, $date, $time) = explode(‘,‘, $contents);  // 把$contents字符串按‘,’分割成数组,然后依次把数组中元素按顺序赋给前面list里的变量。
$date = trim($date, ‘"‘);
$time = trim($time, ‘"‘);

echo "<p>".$symbol." was last sold at: ".$quote."</p>";
echo "<p>Quote current as of ".$date." at ".$time."</p>";

// acknowledge source
echo "<p>This information retrieved from <br /><a href=‘".$url."‘>".$url."</a>.</p>";
?>
</body>
</html>

  

很快,我就遇到了第一个错误:Failure to open http://finance.yahoo.com/d/quotes.csv?s=AMZN&e=.csv$f=sl1d1t1c1ohgv我查了一下这个网址,发现根本打不开,所以应该是网址写错了的缘故,换成下面的就好了:$url = ‘http://finance.yahoo.com/d/quotes.csv‘.    ‘?s=‘ . $symbol . ‘&e=.csv&f=slldltlclohgv‘;然后我刷新界面,发现运行结果如下:"AMZN" was last sold at: "4:00pm - 757.25"Quote current as of 4:00pm - 757.25 at N/AThis information retrieved fromhttp://finance.yahoo.com/d/quotes.csv?s=AMZN&e=.csv&f=slldltlclohgv.然后我点开后面的网址,下载了一个Excel文件:AMZN   4:00pm - <b>757.25</b> 4:00pm - <b>757.25</b> N/A    4:00pm - <b>757.25</b> N/A    4:00pm - <b>757.25</b> -5.1932    4:00pm - <b>757.25</b> 763    763.41 755.36 1744107不是很明白它写的是什么,但是获取的消息跟预想的差不多。书上解释的是:这个程序使用file_get_contents()从URL获取信息,获取到页面文本后,通过list()函数找到我们所要的网页部分(这个例子是去雅虎财经网获取亚马逊的股票报价)(这是一个人对这个例子的解释:In order to write the example code you have, some ardent programmer  went to Yahoo‘s Finance site and observed what happened there when he/she wanted to read the stock quote for Amazon.com. One of the first things *you* should do is visit bankrate.com and find where the LIBOR is displayed. Take a look at the source code around that area, as that‘s what you‘ll be receiving back from a call to file_get_contents().)

这个例子的输出结果为:

这是连接时间超过限制30s了,可以在配置中修改

正常情况下,运行结果为:

"AMZN" was last sold at: "4:00pm - 757.25"Quote current as of 4:00pm - 757.25 at N/AThis information retrieved fromhttp://finance.yahoo.com/d/quotes.csv?s=AMZN&e=.csv&f=slldltlclohgv.
还有一种php写法:
<html>
<head>
    <title>Stock Quote from NASDAQ</title>
</head>
<body>
<?php
// choose stock to look at
$symbol=‘AMZN‘;
echo "<h1>Stock Quote for $symbol</h1>";

$theurl="http://www.amex.com/equities/listCmp/EqLCDetQuote.jsp?Product_Symbol=$symbol";

if (!($contents = file_get_contents($theurl)))
{
    echo ‘Could not open URL‘;
    exit;
}

// find the part of the page we want and output it
$pattern = ‘(\\$[0-9 ]+\\.[0-9]+)‘;

if (eregi($pattern, $contents, $quote))
{
    echo "<p>$symbol was last sold at: ";
    echo $quote[1];
    echo ‘</p>‘;
}
else
{
    echo ‘<p>No quote available</p>‘;
};

// acknowledge source
echo ‘<p>‘
    .‘This information retrieved from <br />‘
    ."<a href=\"$theurl\">$theurl</a><br />"
    .‘on ‘.(date(‘l jS F Y g:i a T‘)).‘</p>‘;
?>
</body>
</html>

  运行结果为:

在PHP中,文件函数还可以完成许多操作,这里给出的例子至少通过http载入一个Web页面,还可以通过https、ftp或其他协议以几乎相同的方式与其他服务进行交互。进入下一个例子:使用网络查找函数。PHP提供一组查找函数,这些函数可以用来检查主机名称、ip地址、邮件交换等信息。例如:如果要创建一个目录站点,例如Yahoo!,当用户提交一个新的URL时,我们可能会自动那个检查URL所指向的主机和此站点的联系信息是否有效。这样,当一个访问者浏览一个网站并发现此网站不存在或电子邮件无效时,可以节省进一步的操作。程序清单 20-2 directory_submit.html —— 提交表单的HTMl
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Submit site</title>
</head>
<body>
<h1>Submit site</h1>
<form method="post" action="directory_submit.php">
    URL: <input type="text" name="url" size="30" value="http://"> <br />
    Email contact: <input type="text" name="email" size="23"> <br />
    <input type="submit" name="Submit site">
</form>
</body>
</html>

  运行结果:

点击提交后显示php页面结果为:

如果用localhost作为URL:

点击提交后:

程序清单 20-3 directory_submit.php —— 用于检查URL和电子邮件地址的脚本
<html>
<head>
    <title>Site submission results</title>
</head>
<body>
<h1>Site submission results</h1>
<?php

// Extract form fields

$url = $_REQUEST[‘url‘];
$email = $_REQUEST[‘email‘];

// Check the URL

$url = parse_url($url);
$host = $url[‘host‘];
if(!($ip = gethostbyname($host)))
{
    echo ‘Host for URL does not have valid IP‘;
    exit;
}

echo "Host is at IP $ip <br>";

// Check the email address

$email = explode(‘@‘, $email);
$emailhost = $email[1];

// note that the getmxrr() function is *not implemented* in
// Windows versions of PHP
if (!getmxrr($emailhost, $mxhostsarr))
{
    echo ‘Email address is not at valid host‘;
    exit;
}

echo ‘Email is delivered via: ‘;
foreach ($mxhostsarr as $mx)
    echo "$mx ";

// If reached here, all ok

echo ‘<br>All submitted details are ok.<br>‘;
echo ‘Thank you for submitting your site.<br>‘
    .‘It will be visited by one of our staff members soon.‘

// In real case, add to db of waiting sites...
?>
</body>
</html>

  

在其php文件中有一些需要注意的地方:· 获取URL并将其作为函数parse_url()的参数。    parse_url()函数是URL解析函数,顾名思义,它可以解析URL,并返回其组成部分:array parse_url ( string url );    返回一个关联数组,包括现有URL的各种组成部分。如果缺少了其中一个,则不会为这个组成部分创建数组项。组成部分有:(以http://nobody:[email protected]:80/script.php?variable = value#anchor 为例)    · scheme : http    · user : noboby    · pass : secret    · host : example.com    · port : 80    · path : /script.php    · query : variable = value (在?之后)    · fragment : anchor (在#之后)· 函数dns_get_mx()检查是否有邮件可以到达的确切地方:dns_get_mx()函数与getmxrr()函数相同,该函数返回一个邮件地址的一组邮件交换(Mail Exchange, MX)记录    bool getmxrr ( string $hostname , array &$mxhosts [, array &$weight ] )    (hostname     The Internet host name.

     mxhosts     A list of the MX records found is placed into the array mxhosts.

     weight     If the weight array is given, it will be filled with the weight information gathered.)     注意:MX记录存储在DNS中,而且查找方式类似于主机名的查找方式。MX列出的机器不一定是邮件最终到达的机器。相反,它是一台知道邮件发送路由的机器(可能不止一台,因此这个函数返回的是一个数组而不是一个主机名字符串)。     例如:[email protected]返回Email is delivered via: 126mx01.mxmail.netease.com 126mx03.mxmail.netease.com 126mx02.mxmail.netease.com 126mx00.mxmail.netease.com 
时间: 2024-08-04 12:05:39

20 使用网络函数和协议函数的相关文章

SQL点滴20—T-SQL中的排名函数

原文:SQL点滴20-T-SQL中的排名函数 提到排名函数我们首先可能想到的是order by,这个是排序,不是排名,排名需要在前面加个名次序号的,order by是没有这个功能的.还可能会想到identity(1,1),它也给了一个序号,但是不能保证给出的序号是连续升序的.除非能够保证所有的Insert语句都能够正确成功地完成,并且没有删除操作,实际的使用中大多数的表都不能保证这样. 好在SQL Server中提供了一些排名函数来辅助实现这些功能.排名函数按照需要的顺序对数据进行排名,并提供一

20.16/20.17 shell中的函数20.18 shell中的数组20.19 告警系统需求分析

20.16/20.17 shell中的函数显示变量的隐函数执行的结果参数只有一个1,执行的结果 第一个是1,第二个是空的,第三个也是空的,肢本的名字是fun1.sh,总共有1个参数加法的函数显示IP的隐函数-A1是显示关键词的一行包括下一行精准查找到IP脚本就可以这样写,ip $eth 是输入一个网卡的名字#!/bin/baship() {ifconfig |grep -A1 "ens33: " |awk '/inet/ {print $2}'}read -p "Please

20.16/20.17 shell中的函数 20.18 shell中的数组 20.19 告警系统需求

20.16/20.17 shell中的函数函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可.格式: function f_name() {? ?? ?? ?? ?? ?? ?? ? command? ?? ?? ?? ? }函数必须要放在最前面示例1 #!/bin/bashinput() {? ? echo $1 $2 $# $0} input 1 a b 示例2 #!/bin/bashsum() {? ? s=$[$1+$2]? ?

FreeRTOS系列第20篇---FreeRTOS信号量API函数

FreeRTOS的信号量包括二进制信号量.计数信号量.互斥信号量(以后简称互斥量)和递归互斥信号量(以后简称递归互斥量).我们可以把互斥量和递归互斥量看成特殊的信号量. 信号量API函数实际上都是宏,它使用现有的队列机制.这些宏定义在semphr.h文件中.如果使用信号量或者互斥量,需要包含semphr.h头文件. 二进制信号量.计数信号量和互斥量信号量的创建API函数是独立的,但是获取和释放API函数都是相同的:递归互斥信号量的创建.获取和释放API函数都是独立的. 1创建二进制信号量 1.1

Java网络编程从入门到精通(20):HTTP协议简介

一.什么是HTTP协议 HTTP协议是一种应用层协议,HTTP是HyperText Transfer Protocol(超文本传输协议)的英文缩写.HTTP可以通过传输层的TCP协议在客户端和服务器之间传输数据.HTTP协议主要用于Web浏览器和Web服务器之间的数据交换.我们在使用IE或Firefox浏览网页或下载Web资源时,通过在地址栏中输入http://host:port/path,开头的4个字母http就相当于通知浏览器使用HTTP协议来和host所确定的服务器进行通讯. HTTP协议

PHP和MySQL Web开发(第4版) by Luke Welling

Web开发是基于HTML的,但是HTML是静态的,于是我们还需要PHP这样动态的服务器端脚本语言,有了PHP还不够,我们还需要把数据存储在MySQL这样的RDBMS(关系数据库管理系统)上,PHP和MySQL都是很成熟的工具,具有高性能.低成本.易用性等优势,是Web开发的明智之选. ==== 目录 ==== 第一篇 使用PHP C01 PHP快速入门 C02 数据的存储与检索 C03 使用数组 C04 字符串操作与正则表达式 C05 代码重用与函数编写 C06 面向对象的PHP C07 错误和

20.16 20.17shell中的函数(上下);20.18 shell中的数组;20.19 告警系统需求分析

20.16 shell中的函数(上) 函数就是把一段代码整理到了一个小单元中,并给这个小单元起 一个名字,当用到这段代码时直接调用这个小单元的名字即可. 1. [[email protected] ~]# vi fun1.sh 添加内容: #!/bin/bash function inp(){ echo "The first par is $1" echo "The second par is $2" echo "The third par is $3&q

20.16-20.17 shell中的函数;20.18 shell中的数组;20.19 告警系统需求

20.16 shell中的函数(上) 函数就是把一段代码整理到了一个小单元中,并给这个小单元起 一个名字,当用到这段代码时直接调用这个小单元的名字即可. 1. [[email protected] ~]# vi fun1.sh 添加内容: #!/bin/bash function inp(){ echo "The first par is $1" echo "The second par is $2" echo "The third par is $3&q

php函数 PHP程序开发参考手册目录

第3章 数组函数库3.1 array_change_key_case函数——返回字符串键名全为小写或大写的数组3.2 array_chunk函数——将一个数组分割成多个数组3.3 array_combine函数——合并数组3.4 array_count_values函数——统计数组中的元素值3.5 array_diff_assoc函数——带索引检查计算数组的差集3.6 array_diff_uassoc函数——用回调函数做索引检查来计算数组的差集3.7 array_diff函数——计算数组的差集