php 中利用 socket curl file_get_contents模仿用户post发送信息
开发中经常用到关于用程序 模仿用户post信息,下面我介介绍了几种方法,有需要的朋友参考一下。
# <?php教程
# /**
# * Socket版本
# * 使用方法:
# * $post_string = "app=socket&version=beta";
# * request_by_socket('facebook.cn','/restServer.php',$post_string);
# */
# function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30){
# $socket = fsockopen($remote_server,$port,$errno,$errstr,$timeout);
# if (!$socket) die("$errstr($errno)");
#
# fwrite($socket,"POST $remote_path HTTP/1.0");
# fwrite($socket,"User-Agent: Socket Example");
# fwrite($socket,"HOST: $remote_server");
# fwrite($socket,"Content-type: application/x-www-form-urlencoded");
# fwrite($socket,"Content-length: ".strlen($post_string)+8."");
# fwrite($socket,"Accept:*/*");
# fwrite($socket,"");
# fwrite($socket,"mypost=$post_string");
# fwrite($socket,"");
#
# $header = "";
# while ($str = trim(fgets($socket,4096))) {
# $header.=$str;
# }
#
# $data = "";
# while (!feof($socket)) {
# $data .= fgets($socket,4096);
# }
#
# return $data;
# }
#
关socket更详细教程请查看
http://www.111cn.net/phper/30/7cadb3c9195ac7d8ac9104da61a25c6e.htm
# /**
# * Curl版本
# * 使用方法:
# * $post_string = "app=request&version=beta";
# * request_by_curl('http://facebook.cn/restServer.php',$post_string);
# */
# function request_by_curl($remote_server,$post_string){
# $ch = curl_init();
# curl_setopt($ch,CURLOPT_URL,$remote_server);
# curl_setopt($ch,CURLOPT_POSTFIELDS,'mypost='.$post_string);
# curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
# curl_setopt($ch,CURLOPT_USERAGENT,"Jimmy's CURL Example beta");
# $data = curl_exec($ch);
# curl_close($ch);
# return $data;
# }
cURL库可以简单和有效地去抓网页,您只需要运行一个脚本,然后分析一下您所抓取的网页,然后就可以以程序的方式得到您想要的数据了。无论是您想从一个链接上取部分数据,或是取一个XML文件并把其导入数据库教程,哪怕就是简单的获取网页内容,cURL是一个功能强大的PHP库。本文主要讲述如果使用这个PHP库。
curl参考文献
http://www.111cn.net/phper/18/curl-php.htm
# /**
# * 其它版本
# * 使用方法:
# * $post_string = "app=request&version=beta";
# * request_by_other('http://facebook.cn/restServer.php',$post_string);
# */
# function request_by_other($remote_server,$post_string){
# $context = array(
# 'http'=>array(
# 'method'=>'POST',
# 'header'=>'Content-type: application/x-www-form-urlencoded'."".
# 'User-Agent : Jimmy's POST Example beta'."".
# 'Content-length: '.strlen($post_string)+8,
# 'content'=>'mypost='.$post_string)
# );
# $stream_context = stream_context_create($context);
# $data = file_get_contents($remote_server,FALSE,$stream_context);
# return $data;
# }
#
# ?>
file_get_contents() 函数把整个文件读入一个字符串中。
和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串。
file_get_contents() 函数是用于将文件的内容读入到一个字符串中的首选方法。如果操作系统支持,还会使用内存映射技术来增强性能。
参考文献
http://www.111cn.net/phper/24/f8d52eaae81ea27383375ead36ffbd4d.htm
数据库教程
--
-- Table structure for table `category`
--
CREATE TABLE IF NOT EXISTS `category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`catpath` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;
--
-- Dumping data for table `category`
--
INSERT INTO `category` (`id`, `catpath`, `name`) VALUES
(1, '0', '网站首页'),
(2, '0-1', 'Linux OS'),
(3, '0-1', 'Apache服务器'),
(4, '0-1', 'MySQL数据库'),
(5, '0-1', 'PHP脚本语言'),
(6, '0-1-2', 'Linux 系统教程'),
(7, '0-1-2', 'Linux 网络技术'),
(8, '0-1-2', 'Linux 安全基础'),
(9, '0-1-2-7', 'Linux LAMP'),
(10, '0-1-3-10', 'apache Server');
php教程代码
$conn = mysql教程_connect ( 'localhost', 'root', '' );
mysql_select_db ( 'test', $conn );
mysql_query ( 'set names UTF8' );
$sql = "select id,concat(catpath,'-',id) as abspath,name from category order by abspath";
$query = mysql_query ( $sql );
while ( $row = mysql_fetch_array ( $query ) ) {
/**
* 第一种展示方法
*/
/*$space = str_repeat ( ' ', count ( explode ( '-', $row ['abspath'] ) ) - 1 );
echo $space . $row ['name'] . '<br>';*/
/**
* 第二种展示方法
*/
$space = str_repeat ( ' ', count ( explode ( '-', $row ['abspath'] ) ) - 1 );
$option .= '<option value="' . $row ['id'] . '">' . $space . $row ['name'] . '</option>';
}
echo '<select name="opt">' . $option . '</select>';
效果
php教程显示文章 几分钟前,几小时前,几天前发布类
文章发表时的UNIX时间戳,来转化为例如 几分钟前,几小时前,几天前 这样的提示。如微博
这看起来更加人性化
1 <?php
2 class timeAgo
3 {
4 static $timeagoObject;
5 private $rustle;
6 private $unit;
7
8 private function __construct()
9 {
10
11 }
12 private function __clone(){ }
13 public static function getObject()
14 {
15 if(! (self::$timeagoObject instanceof self) )
16 self::$timeagoObject = new timeAgo();
17
18 return self::$timeagoObject;
19 }
20 private function count_int($unix_C) // main function
21 {
22 if(! (isset($unix_C) || is_numeric($unix_C)) )
23 return 'don't find parameter';
24
25 $d = time()-$unix_C ; // $d - unix time difference value
26 $d_int =(int)floor($d/60) ; // minimum unit -- minutes unix/60
27
28 $this->unit = 0 ; // is minutes,hour or day?
29
30 if($d_int < 60){ // minutes in one hour 3600
31 $this->rustle = $d_int;
32 $this->unit = 1;
33 }
34 else if($d_int < 720){ //hour in one day 3600*12
35 $this->rustle = floor($d_int/60);
36 $this->unit = 2 ;
37 }
38 else if($d_int < 7200){ //day in ten days 3600*12*10
39 $this->rustle = floor($d_int/720);
40 $this->unit = 3 ;
41 }
42 else{
43 $this->rustle = $d ;
44 $this->unit = 4 ;
45 }
46 }
47 public function piece_str($C)
48 {
49 $this->count_int($C);
50
51 $u = '';
52 switch( $this->unit )
53 {
54 case 1:
55 $u = 'minute';
56 break;
57 case 2:
58 $u = 'hour';
59 break;
60 case 3:
61 $u = 'day';
62 break;
63 case 4:
64 $u = '';
65 break;
66 case 0:
67 return 'sorry , get time is fail';
68 }
69 if($this->unit < 4)
70 {
71 if($this->rustle > 1)
72 return (string)$this->rustle.$u.'s ago';
73 else if($this->rustle == 1)
74 return (string)$this->rustle.$u.'ago';
75 else
76 return 'Just now';
77 }
78 }
79 /* example: $ago = timeAgo::getObject();
80 * echo $ago->piece_str($unix);
81 * // 2 days ago
82 */
83 }
84 ?>
php教程 代码运行时间查看类
//date:2011-08-05
class RunTime//页面执行时间类
{
private $starttime;//页面开始执行时间
private $stoptime;//页面结束执行时间
private $spendtime;//页面执行花费时间
function getmicrotime()//获取返回当前微秒数的浮点数
{
list($usec,$sec)=explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
function start()//页面开始执行函数,返回开始页面执行的时间
{
$this->starttime=$this->getmicrotime();
}
function end()//显示页面执行的时间
{
$this->stoptime=$this->getmicrotime();
$this->spendtime=$this->stoptime-$this->starttime;
//return round($this->spendtime,10);
}
function display()
{
//$this->end();
echo "<p>运行时间:".round($this->spendtime,10)."秒</p>";
}
}
/*调用方法
$timer=new Runtime();
$timer->start();
$timer->end();
view sourceprint?
$timer->display();
要做时间数据转换为时间戳我们利用php教程自带函数,strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳
格式一 Fri Aug 05 12:19:04 +0800 2011
strtotime("Fri Aug 05 12:19:04 +0800 2011");
格式二 2011-02-01
strtotime("2011-02-01");
其它参考
<?php
echo(strtotime("now"));
echo(strtotime("3 October 2005"));
echo(strtotime("+5 hours"));
echo(strtotime("+1 week"));
echo(strtotime("+1 week 3 days 7 hours 5 seconds"));
echo(strtotime("next Monday"));
echo(strtotime("last Sunday"));
?>输出:
1138614504
1128290400
1138632504
1139219304
1139503709
1139180400
1138489200
好了关于strtotime函数的实例也就这么多了,有需要的朋友可以参考一下。
相关文章
php 中file_get_contents超时问题的解决方法
file_get_contents超时我知道最多的原因就是你机器访问远程机器过慢,导致php脚本超时了,但也有其它很多原因,下面我来总结file_get_contents超时问题的解决方法总结。...2016-11-25- php 获取用户IP与IE信息程序 function onlineip() { global $_SERVER; if(getenv('HTTP_CLIENT_IP')) { $onlineip = getenv('HTTP_CLIENT_IP');...2016-11-25
php file_get_contents 设置代理抓取页面示例
file_get_contents函数在php中可以直接打开本地文件也可以直接抓取远程服务器文件,如果简单的采集我们可以使用file_get_contents直接来操作,如果有防采集我们可能需要...2016-11-25- php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p> </p> <p> </p> <form name="form1...2016-11-25
php报错file_get_contents(): php_network_getaddresses问题
本文章来为各位介绍一篇关于file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known...错误解决办法。 昨天,服务器的DN...2016-11-25- 当来访者浏览器语言是中文就进入中文版面,国外的用户默认浏览器不是中文的就跳转英文页面。 <?php $lan = substr( $HTTP_ACCEPT_LANGUAGE,0,5); if ($lan == "zh-cn") print("<meta http-equiv='refresh' c...2015-11-08
- file_get_contents的超时处理话说,从PHP5开始,file_get_content已经支持context了(手册上写着:5.0.0 Added the context support. ),也就是说,从5.0开始,file_get_contents其实也可以POST数据。今天说的这篇是讲超时的,确实在...2013-10-04
- 一个用Javascript检测用户输入密码强度的效果代码,以下代码主要是从以下四个方面检测用户输入的密码的强度的,有兴趣的朋友可以自己添加或修改成自己想要的形式! 1. 如果输入的密码位数少于5位,那么就判定为弱。 2. 如果...2015-10-23
- 【问题描述】:同一用户在同一时间多次登录如果不能检测出来,是危险的。因为,你无法知道是否有其他用户在登录你的账户。如何禁止同一用户多次登录呢? 【解决方案】 (1) 每次登录,身份认证成功后,重新产生一个session_id。 s...2015-11-24
file_get_contents()获取https出现这个错误Unable to find the wrapper “https”
下面我们来看一篇关于file_get_contents()获取https出现这个错误Unable to find the wrapper “https”问题的解决办法. file_get_contents()获取https出现这个错...2016-11-25- 这是注册程序是一款当用户输入完用户名是,就会自动去数据库中查询用户要注册的用户名是否己经被注册了,如果是返回提示否则提示可以注册。 conn.php文件 代...2016-11-25
- 这篇文章主要给大家介绍了关于微信小程序用户授权最佳实践的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-05-08
- 在使用file_put_contents时会碰到并发性问题了,对于这个问题我们有多种解决方案了,其实锁是小编比较喜欢的解决办法了,当然也有其它办法,具体如下。 解决 办法一,fil...2016-11-25
- cookie 的用途之一是存储用户在特定网站上的密码和 id。另外,也用于存储起始页的首选项。在提供个人化查看的网站上,将要求阁下的网络浏览器利用阁下计算机硬驱上的少量...2016-11-25
- 注册页面是大多数网站必备的页面,所以很有必要对自己的注册页面做些精心的设计。下面三张图,第一张是注册的展示页面,第二张思维导图就一个简单的逻辑,第三张是通过firebug查看调用的JS文件。 一、给每个输入框写下说明在...2015-11-24
php提示Warning: file_get_contents(): couldn’t resolve
在使用file_get_contents函数获取远程文件时提示Warning: file_get_contents(): couldn’t resolve错误了,这个我们可以看出是dns的问题,解决办法也简单。 今天在...2016-11-25- 知识归纳因为MySQL是使用User和Host两个字段来确定用户身份的,这样就带来一个问题,就是一个客户端到底属于哪个host。 如果一个客户端同时匹配几个Host,对用户的确定将按照下面的优先级来排 基本观点越精确的匹配越优先...2015-11-08
- 这篇文章主要介绍了sqlserver添加sa用户和密码的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-05-07
- 第一,网站的内容;请各位站长朋友不要一天到晚只想着出什么好的绝招来推广网站,却忽略了网站的内容;其实网站的内容是极为重要的,因为这是你的本,你的根!网站的内容只有不断...2017-07-06
- 装完数据库清理一些默认账号的时候不小心把root删除了,flush privileges 之后的新 root 忘了grant任何权限,查看mysqld选项里面有个 −−skip-grant-tables复制代码 代码如下: #/usr/libexec/mysqld --verbos...2015-03-15