php curl模拟post提交数据示例
程序员文章站
2023-12-04 08:29:34
复制代码 代码如下:
<?
header("content-type: text/html; charset=utf8");
/*
* 提交请求
* @param $header array 需要配置的域名等header设置 array("host: devzc.com");
* @param $data string 需要提交的数据 'user=xxx&qq=xxx&id=xxx&post=xxx'....
* @param $url string 要提交的url 'http://192.168.1.12/xxx/xxx/api/';
*/
function curl_post($header,$data,$url)
{
$ch = curl_init();
$res= curl_setopt ($ch, curlopt_url,$url);
var_dump($res);
curl_setopt($ch, curlopt_ssl_verifyhost, false);
curl_setopt($ch, curlopt_ssl_verifypeer, false);
curl_setopt ($ch, curlopt_header, 0);
curl_setopt($ch, curlopt_post, 1);
curl_setopt($ch, curlopt_postfields, $data);
curl_setopt ($ch, curlopt_returntransfer, 1);
curl_setopt($ch,curlopt_httpheader,$header);
$result = curl_exec ($ch);
curl_close($ch);
if ($result == null) {
return 0;
}
return $result;
}
复制代码 代码如下:
<?
header("content-type: text/html; charset=utf8");
/*
* 提交请求
* @param $header array 需要配置的域名等header设置 array("host: devzc.com");
* @param $data string 需要提交的数据 'user=xxx&qq=xxx&id=xxx&post=xxx'....
* @param $url string 要提交的url 'http://192.168.1.12/xxx/xxx/api/';
*/
function curl_post($header,$data,$url)
{
$ch = curl_init();
$res= curl_setopt ($ch, curlopt_url,$url);
var_dump($res);
curl_setopt($ch, curlopt_ssl_verifyhost, false);
curl_setopt($ch, curlopt_ssl_verifypeer, false);
curl_setopt ($ch, curlopt_header, 0);
curl_setopt($ch, curlopt_post, 1);
curl_setopt($ch, curlopt_postfields, $data);
curl_setopt ($ch, curlopt_returntransfer, 1);
curl_setopt($ch,curlopt_httpheader,$header);
$result = curl_exec ($ch);
curl_close($ch);
if ($result == null) {
return 0;
}
return $result;
}
$url = 'http://127.0.0.1' ;
$header = array("host:127.0.0.1",
"content-type:application/x-www-form-urlencoded",
'referer:http://127.0.0.1/toolindex.xhtml',
'user-agent: mozilla/4.0 (compatible; msie .0; windows nt 6.1; trident/4.0; slcc2;)');
$data = 'tools_id=1&env=gamma';
echo "argv:$data<br>";
$ret = curl_post($header, $data,$url);
$utf8 = iconv('gb2312', 'utf-8//ignore', $ret);
echo 'return:<br>'.nl2br($utf8 ).'<br>';
?>