php模拟post提交请求,curl调用接口-古蔺大橙子建站
RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
php模拟post提交请求,curl调用接口
/**
     * 模拟post进行url请求
     * @param string $url
     * @param array $post_data
     */
    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;
    }

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

宝应网站建设公司成都创新互联公司,宝应网站设计制作,有大型网站制作公司丰富经验。已为宝应千余家提供企业网站建设服务。企业网站搭建\外贸营销网站建设要多少钱,请找那个售后服务好的宝应做网站的公司定做!

function testAction(){
        $url = 'http://mobile.jschina.com.cn/jschina/register.php';
        $post_data['appid']       = '10';
        $post_data['appkey']      = 'cmbohpffXVR03nIpkkQXaAA1Vf5nO4nQ';
        $post_data['member_name'] = 'zsjs124';
        $post_data['password']    = '123456';
        $post_data['email']    = 'zsjs124@126.com';
        //$post_data = array();
        $res = $this->request_post($url, $post_data);       
        print_r($res);

    }

当前标题:php模拟post提交请求,curl调用接口
分享网址:http://scgulin.cn/article/jhsdod.html