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     }
  • 相关阅读:
    open_basedir restriction in effect的错误及其解决办法
    SNMP-网络管理协议
    安装cacti监控系统
    并发时-修改Linux系统下的最大文件描述符限制
    js new date()说明
    阿里云ECS环境部署 centos 6.5
    sysbench
    http_load
    LeetCode: Spiral Matrix
    LeetCode:Length of Last Word
  • 原文地址:https://www.cnblogs.com/it80/p/4411309.html
Copyright © 2011-2022 走看看