zoukankan      html  css  js  c++  java
  • 模拟post请求(PHP)

     1 <?php
     2     
     3     //=========================模拟post请求====================================
     4    // ===========================================================
     5     /**
     6      * 模拟post进行url请求
     7      * @param string $url
     8      * @param array $post_data
     9      */
    10     function request_post($url = '', $post_data = array()) {
    11         if (empty($url) || empty($post_data)) {
    12             return false;
    13         }
    14         
    15         $o = "";
    16         foreach ( $post_data as $k => $v ) 
    17         { 
    18             $o.= "$k=" . urlencode( $v ). "&" ;
    19         }
    20         $post_data = substr($o,0,-1);
    21 
    22         $postUrl = $url;
    23         $curlPost = $post_data;
    24         $ch = curl_init();//初始化curl
    25         curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
    26         curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
    27         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    28         curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    29         curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    30         $data = curl_exec($ch);//运行curl
    31         curl_close($ch);
    32         
    33         return $data;
    34     }
    35     
    36     public function actionPost(){
    37         $url = 'http://XXXXXXXXX';
    38         $post_data['username']       = '2015ceshi';
    39         $post_data['password']      = '123456';
    40         
    41         //$post_data = array();
    42         $res = $this->request_post($url, $post_data);       
    43         print_r($res);
    44 
    45     }
  • 相关阅读:
    微信小程序之:获得appid
    小程序v0.10基本布局
    小程序v0.02 清理干净
    微信小程序v0.01
    让Eclipse在10秒内启动的7个优化提速技巧
    转 1 年经验 Java 求职面试题
    坑爹啊
    ES6,时间格式yyyy-MM-dd HH:MM:SS
    ln: creating hard link 问题
    boost实现串口通信(一):小试牛刀
  • 原文地址:https://www.cnblogs.com/it80/p/4411309.html
Copyright © 2011-2022 走看看