PHP模拟http请求
方法一:利用php的socket编程来直接给接口发送数据来模拟post的操作。建立两个文件post.php,getpost.php
post.php内容如下:
getpost.php的内容如下
结果输出:
this is the data postedArray
(
=> abc
=> how are you , my friend??
)
以上代码在本机81端口下已经通过测试。
方法二:
使用PHP的curl扩展或HttpClient.class.php类,这两个非常类似,下面简单的列出curl的实现代码。
两个文件post2.php和getpost2.php
post2.php的内容如下:
getpost2.php的内容如下:
结果输出:
Array
(
=> returndataArray
(
=> ’wwwbaiducom’
=> 123456
=> check
=> ’NDE005’
=> 1
)
)
方法三:这个要借助第三方类库HttpClient可以到这里下载:http://scripts.incutio.com/httpclient/
<?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);
?>
页:
[1]