首页 > 编程 > PHP > 正文

PHP模拟POST请求

2019-11-14 10:46:22
字体:
来源:转载
供稿:网友
方式一:用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;  }   $url = 'http://www.dybcotton.com/search/exPRess'; $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();//初始化CURLcurl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页curl_setopt($ch, CURLOPT_HEADER, 0);//设置HEADERcurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上curl_setopt($ch, CURLOPT_POST, 1);//POST提交方式curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);$data = curl_exec($ch);//运行CURLcurl_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 ;
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表