php+模拟get+请求,PHP模拟 HTTP GET POST请求示例

PHP模拟 HTTP GET POST请求示例

发布于 2015-04-17 11:55:48 | 259 次阅读 | 评论: 0 | 来源: 网友投递

PHP开源脚本语言PHP(外文名: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,入门门槛较低,易于学习,使用广泛,主要适用于Web开发领域。PHP的文件后缀名为php。

PHP有三种方法可以模拟POST提交,分别为curl、socket、file_get_contents

curl方式:(我目前用的是这种)

/**

* 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,$post_string);

curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

$data = curl_exec($ch);

curl_close($ch);

return $data;

}

socket方式:

/**

* 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.0rn");

fwrite($socket,"User-Agent: Socket Examplern");

fwrite($socket,"HOST: $remote_serverrn");

fwrite($socket,"Content-type: application/x-www-form- urlencodedrn");

fwrite($socket,"Content-length: ".strlen($post_string)+8."rn");

fwrite($socket,"Accept:*/*rn");

fwrite($socket,"rn");

fwrite($socket,"mypost=$post_stringrn");

fwrite($socket,"rn");

$header = "";

while ($str = trim(fgets($socket,4096))) {

$header.=$str;

}

$data = "";

while (!feof($socket)) {

$data .= fgets($socket,4096);

}

return $data;

}

file_get_contents方式:

/**

* file_get_contents版本

* 使用方法:

* $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‘."rn".

‘User-Agent : Jimmy’s POST Example beta‘."rn".

‘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;

}

<?php

require_once ‘HttpClient.class.php’;

$params = array(’web’ => ’www.baidu.com’,

’pwd’ => ’123456’,

’action’ => ’check’,

’pseid’ => ’NDE005’,

’amt’ => 1);

$pageContents = HttpClient::quickPost(’http://localhost:81/flandy/getpost3.php’, $params);

$result = explode(’,’, $pageContents);

print_r($result);

?>

相关阅读:

PHP模拟 HTTP GET POST请求示例

PHP中Http协议post请求参数

php中用socket模拟http中post或者get提交数据的示例代码

PHP实现支持GET,POST,Multipart/form-data的HTTP请求类

PHP使用curl方式取得数据、模拟登陆、POST数据示例详解

php发送post请求示例代码

php 模拟get_headers函数的代码示例

使用PHP模拟HTTP认证

PHP中使用file_get_contents post数据示例代码

php利用fsockopen GET/POST提交表单及上传文件

PHP 使用Socket 编程模拟Http post和get请求的方法

php使用curl模拟get,post示例代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值