当前位置:编程学习 > php >>

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.zzzyk.com/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.zzzyk.com/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.zzzyk.com/phper/24/f8d52eaae81ea27383375ead36ffbd4d.htm

补充:Php教程,Php常用代码 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,