PHP 使用 Post 方式请求网页数据入门实例

内容摘要
这篇文章主要为大家详细介绍了PHP 使用 Post 方式请求网页数据入门实例,具有一定的参考价值,可以用来参考一下。

对PHP 以 Post 方式请求网页数据对此感兴趣的朋友,看看idc笔
文章正文

这篇文章主要为大家详细介绍了PHP 使用 Post 方式请求网页数据入门实例,具有一定的参考价值,可以用来参考一下。

对PHP 以 Post 方式请求网页数据对此感兴趣的朋友,看看idc笔记做的技术笔记!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<code class="php">
/**
 * 以 Post 方式请求网页数据
 *
 * @param
 * @arrange 五一二笔记网: 512pic.com
* Post 方式请求网页数据
*
* @param string $url     网页地址
* @prarm string $host    主机
* @param string $session 会话值
* @prarm string $type    类型(POST、GET)
* @prarm string $port    端口
* @prarm string $data    数据
*/
function getPageConent( $url, $host$data = "" ,$session = "", $type = "POST", $port = "") {
  if( empty($port) ) $port = 80;    
  /* 请求数据 */
  $post_data = $data;
 $lenght = strlen($post_data);
 $headers  = "{$type} {$url} HTTP/1.1\r\n";
 $headers .= "Accept: * /*\r\n";
 $headers .= "Content-Type: application/x-www-form-urlencoded\r\n";
 $headers .= "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB6; CIBA; .NET CLR 4.0.20506)\r\n";
 if($session != "" ) $headers .= "Cookie:JSESSIONID={$session}\r\n";
 $headers .= "Host: {$host}:{$port}\r\n";
 $headers .= "Content-Length: {$lenght}\r\n";
 $headers .= "Connection: Close\r\n\r\n";
 $headers .= $post_data;
 if( $fp = fsockopen( $host, $port, $errno, $errstr, 100) ) {
     fwrite($fp, $headers);       
       $header   = fread($fp, 1024);   
       $content  = fread($fp, 1024);   
       $content .= fread($fp, 1024);
       $content .= fread($fp, 1024);
       $content .= fread($fp, 1024);
     fclose($fp);       
 }   
 if( $data != "" ) {
     echo $headers;
     echo "<hr />";
     echo $header;
     echo "<hr />";
     echo $content;
     echo "<hr />";
     exit;
 } else {
     return $content;
 }   
}
/***   来自php教程(www.idcnote.com)   ***/</code>

注:关于PHP 使用 Post 方式请求网页数据入门实例的内容就先介绍到这里,更多相关文章的可以留意

代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!