PHP模拟POST请求

方式一:用file_get_contents来模拟请求:
function file_get_contents_post($url = '', $post_data = array()) {  
    $options = array(  
        'http' => array(  
            'method' => 'POST',  
            'content' => http_build_query($post_data),  
        ),  
    );  
    $result = file_get_contents($url, false, stream_context_create($options));  
    return $result;  
}  

 $post_data['search'] ='65311151191';
 $res = file_get_contents_post($url, $post_data);     
 print_r($res); 
 
 方式二:curl来模拟请求: 
function request_post($url = '', $post_data = array()) {
if (empty($url) || empty($post_data)) {
return false;
}
$o = "";
foreach ( $post_data as $k => $v ) 
$o.= "$k=" . urlencode( $v ). "&" ;
}
$post_data = substr($o,0,-1);

$postUrl = $url;
$curlPost = $post_data;
$ch = curl_init();//初始化CURL
curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0);//设置HEADER
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1);//POST提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);//运行CURL
curl_close($ch);
return $data;
}

 $url = 'http://www.dybcotton.com/search/express';
 $post_data['search'] ='65311151191';
 $res = request_post($url, $post_data);     
 print_r($res); 

search参数需要我们自己去抓包获得 
常用抓包工具下载地址:
http://www.fiddler2.com/dl/Fiddler2Setup.exe ;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值