标签:代理服务器 服务器 comm error code 微软 return float gets
HTTP请求:请求行、消息报头、请求正文。格式如下:
Method Request-URI HTTP-Veraion CRLF
参数说明
响应:状态行、消息报头、响应正文
HTTP-Version Status-Code Reason-Phrase CRLF
参数说明
常见状态码
get_headers(string $url [, int $format = 0 ] ) — 取得服务器响应一个 HTTP 请求所发送的所有标头
返回包含有服务器响应一个 HTTP 请求所发送标头的索引或关联数组,如果失败则返回 FALSE 。 (通过判断状态码是否为200,就可以判断请求的资源存在与否)
3. 发送报文
GET /test/httptest.php?id=1 HTTP/1.1
Connection: close
Host: localhost
Content-type:application/x-www-form-urlencoded
content-length:20
(两个回车)
#通过1.1版本协议请求index.html页面;connection: close是实用短连接,即服务器返回后就断开连接;Host字段知名页面所在的主机名。
测试服务端 httptest.php
<?php
$dataGet = $_GET;
$dataPost = $_POST;
$dataGetstr = http_build_query($dataGet);
$dataPoststr = http_build_query($dataPost);
if( $dataGet ){
echo ‘get ‘.$dataGetstr;
}
if( $dataPost ){
echo ‘post ‘.$dataPoststr;
}
?>
PHP实现HTTP请求
$postData =http_build_query(
array(
‘title‘ => "这里是 file_get_contents 提交的数据",
‘content‘ => "你好 !",
‘type‘ => 1
)
);
#file_get_contents
$opts = array(
‘http‘ =>array(
‘method‘ => "POST" ,
‘header‘ => "Host:localhost\r\n".
"Content-type:application/x-www-form-urlencoded\r\n".
"Content-length:".(strlen($postData))."\r\n".
"Cookie: foo=bar\r\n",
"content" => $postData,
//‘timeout‘ => 60 * 60 // 超时时间(单位:s)
)
);
$context = stream_context_create ( $opts );//创建数据流上下文
//file_get_contents( ‘http://localhost/test/httptest.php‘,false,$context );
#fopen
$postData = http_build_query(
array(
‘title‘ => "这里是 fopen 提交的数据",
‘content‘ => "你好 !",
‘type‘ => 1
));
$fp = fopen( ‘http://localhost/test/httptest.php‘,‘r‘,false,$context );
curl方式提交
$url = "http://localhost/test/httptest.php";
$postData = http_build_query(
array(
‘title‘ => "这里是 fopen 提交的数据",
‘content‘ => "你好 !",
‘type‘ => 1
));
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL,$url );
curl_setopt( $ch,CURLOPT_POST,1 );
curl_setopt( $ch,CURLOPT_POSTFIELDS,$postData );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER,1 );
curl_exec();
curl_close();
socket方式提交
$postData = http_build_query(
array(
‘title‘ => "这里是 fopen 提交的数据",
‘content‘ => "你好 !",
‘type‘ => 1
));
$fp = fsockopen( "localhost",80,$errno,$errorStr,5 );
$request = "POST http://localhost/test/httptest.php HTTP/1.1\r\n";
$request .= "Host:localhost\r\n";
$request .= "Content-type:application/x-www-form-urlencoded\r\n";
$request .= "Content-length:".(strlen($postData))."\r\n";
$request .= $postData;
fwrite( $fp,$request );
while(!feof($fp)){
echo fgets($fp,1024);
}
fclose($fp);
-- 这段时间读读书,理解理解原理,生活也很充实。
标签:代理服务器 服务器 comm error code 微软 return float gets
原文地址:http://www.cnblogs.com/followyou/p/7004502.html