当前位置:首页 > PHP教程 > php应用 > 列表

PHP模拟post提交数据方法汇总

发布:smiling 来源: PHP粉丝网  添加日期:2021-07-10 10:54:14 浏览: 评论:0 

使用php模拟post传值虽然在日常生活中用到的不是很多,但是在某些场合还是经常用到的,下面脚本之家小编给大家整理了三种php模拟post传值的方法,file_get_contents、curl和socket,需要的朋友参考下

使用php模拟post传值虽然在日常生活中用到的不是很多,但是在某些场合还是经常用到的。下面脚本之家小编给大家整理了三种php模拟post传值的方法,file_get_contents、curl和socket。

第一种:file_get_contents来模拟post

  1. <php 
  2.  
  3. function file_get_contents_post($url, $post){ 
  4. $options = array( 
  5. ‘http‘=> array( 
  6. ‘method‘=>‘POST‘, 
  7. ‘content‘=> http_build_query($post), 
  8. ), 
  9. ); 
  10. $result = file_get_contents($url,false, stream_context_create($options)); 
  11. return $result; 
  12. } 
  13. $data = file_get_contents_post("http://www.a.com/post/post.php", array(‘name‘=>‘caiknife‘,‘email‘=>‘caiknife#gmail.com‘)); 
  14. var_dump($data); 

第二种:curl模拟post

  1. <php 
  2. function curl_post($url, $post){ 
  3. $options = array( 
  4. CURLOPT_RETURNTRANSFER =>true, 
  5. CURLOPT_HEADER =>false, 
  6. CURLOPT_POST =>true, 
  7. CURLOPT_POSTFIELDS => $post, 
  8. ); 
  9. $ch = curl_init($url); 
  10. curl_setopt_array($ch, $options); 
  11. $result = curl_exec($ch); 
  12. curl_close($ch); 
  13. return $result; 
  14. } 
  15. $data = curl_post("http://www.a.com/post/post.php", array(‘name‘=>‘caiknife‘,‘email‘=>‘caiknife#gmail.com‘)); 
  16. var_dump($data); 

第三种:socket来模拟post

  1. <php 
  2. function socket_post($url, $post){ 
  3. $urls = parse_url($url); 
  4. if(!isset($urls[‘port‘])){ 
  5. $urls[‘port‘]=80; 
  6. } 
  7. $fp = fsockopen($urls[‘host‘], $urls[‘port‘], $errno, $errstr); 
  8. if(!$fp){ 
  9. echo "$errno, $errstr"; 
  10. exit(); 
  11. } 
  12. $post = http_build_query($post); 
  13. $length = strlen($post); 
  14. $header =<<<HEADER 
  15. <span class="Apple-tab-span" style="white-space:pre"></span>POST {$urls[‘path‘]} HTTP/1.1 
  16. <span class="Apple-tab-span" style="white-space:pre"></span>Host:{$urls[‘host‘]} 
  17. <span class="Apple-tab-span" style="white-space:pre"></span>Content-Type: application/x-www-form-urlencoded 
  18. <span class="Apple-tab-span" style="white-space:pre"></span>Content-Length:{$length} 
  19. <span class="Apple-tab-span" style="white-space:pre"></span>Connection: close 
  20. <span class="Apple-tab-span" style="white-space:pre"></span>{$post} 
  21. <span class="Apple-tab-span" style="white-space:pre"></span>HEADER; 
  22. fwrite($fp, $header); 
  23. $result =‘‘; 
  24. while(!feof($fp)){ 
  25. $result .= fread($fp,512); 
  26. } 
  27. $result = explode("\r\n\r\n", $result,2); 
  28. return $result[1]; 
  29. } 
  30. $data = socket_post("http://www.a.com/post/post.php", array(‘name‘=>‘caiknife‘,‘email‘=>‘caiknife#gmail.com‘)); 
  31. var_dump($data); 

上面这三种方法最后看到的内容都是一样的,都可以得到post的传值;但是在是用socket的时候,发送header信息时必须要注意header的完整信息,比如content type和content length必须要有,connection: close和post数据之间要空一行,等等;而通过socket取得的内容是包含了header信息的,要处理一下才能获得真正的内容。

下面给大家说下php模拟post提交请求,调用接口

  1. /** 
  2. * 模拟post进行url请求 
  3. * @param string $url 
  4. * @param string $param 
  5. */ 
  6. function request_post($url = '', $param = '') { 
  7. if (emptyempty($url) || emptyempty($param)) { 
  8. return false; 
  9. } 
  10. $postUrl = $url; 
  11. $curlPost = $param; 
  12. $ch = curl_init();//初始化curl 
  13. curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页 
  14. curl_setopt($ch, CURLOPT_HEADER, 0);//设置header 
  15. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 
  16. curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 
  17. curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); 
  18. $data = curl_exec($ch);//运行curl 
  19. curl_close($ch); 
  20. return $data; 
  21. } 

这是方法,下面是具体的调用案例。

  1. function testAction(){ 
  2. $url = 'http://mobile.jschina.com.cn/jschina/register.php'; 
  3. $post_data['appid'] = '10'; 
  4. $post_data['appkey'] = 'cmbohpffXVR03nIpkkQXaAA1Vf5nO4nQ'; 
  5. $post_data['member_name'] = 'zsjs123'; 
  6. $post_data['password'] = '123456'; 
  7. $post_data['email'] = 'zsjs123@126.com'; 
  8. $o = ""; 
  9. foreach ( $post_data as $k => $v )  
  10. {  
  11. $o.= "$k=" . urlencode( $v ). "&" ; 
  12. } 
  13. $post_data = substr($o,0,-1); 
  14. $res = $this->request_post($url, $post_data);  
  15. print_r($res); 
  16. } 

这样就提交请求,并且获取请求结果了。一般返回的结果是json格式的。

这里的post是拼接出来的,也可以改造成下面的方式。

  1. /** 
  2. * 模拟post进行url请求 
  3. * @param string $url 
  4. * @param array $post_data 
  5. */ 
  6. function request_post($url = '', $post_data = array()) { 
  7. if (emptyempty($url) || emptyempty($post_data)) { 
  8. return false; 
  9. } 
  10. $o = ""; 
  11. foreach ( $post_data as $k => $v )  
  12. {  
  13. $o.= "$k=" . urlencode( $v ). "&" ; 
  14. } 
  15. $post_data = substr($o,0,-1); 
  16. $postUrl = $url; 
  17. $curlPost = $post_data; 
  18. $ch = curl_init();//初始化curl 
  19. curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页 
  20. curl_setopt($ch, CURLOPT_HEADER, 0);//设置header 
  21. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 
  22. curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 
  23. curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); 
  24. $data = curl_exec($ch);//运行curl 
  25. curl_close($ch); 
  26. return $data; 
  27. } 

将拼接也封装了起来,这样调用的时候就更简洁了。

  1. function testAction(){ 
  2. $url = 'http://mobile.jschina.com.cn/jschina/register.php'; 
  3. $post_data['appid'] = '10'; 
  4. $post_data['appkey'] = 'cmbohpffXVR03nIpkkQXaAA1Vf5nO4nQ'; 
  5. $post_data['member_name'] = 'zsjs124'; 
  6. $post_data['password'] = '123456'; 
  7. $post_data['email'] = 'zsjs124@126.com'; 
  8. //$post_data = array(); 
  9. $res = $this->request_post($url, $post_data);  
  10. print_r($res); 
  11. }

Tags: PHP模拟post

分享到: