php模拟post提交请求,调用接口

简介:
 /**
     * 模拟post进行url请求
     * @param string $url
     * @param string $param
     */
    function request_post($url = '', $param = '') {
        if (empty($url) || empty($param)) {
            return false;
        }
        
        $postUrl = $url;
        $curlPost = $param;
        $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'] = 'zsjs123';
        $post_data['password']    = '123456';
        $post_data['email']    = 'zsjs123@126.com';
        $o = "";
        foreach ( $post_data as $k => $v ) 
        { 
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        $post_data = substr($o,0,-1);

        $res = $this->request_post($url, $post_data);       
        print_r($res);

    }
复制代码

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

这里的post是拼接出来的。

 

也可以改造成下面的方式。

复制代码
/**
     * 模拟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);

    }



相关文章
|
2天前
|
监控 PHP Python
1688快速获取整店铺列表 采集接口php Python
在电子商务的浪潮中,1688平台作为中国领先的批发交易平台,为广大商家提供了一个展示和销售商品的广阔舞台;然而,要在众多店铺中脱颖而出,快速获取商品列表并进行有效营销是关键。
|
9天前
|
PHP 数据安全/隐私保护
【PHP开发专栏】PHP接口与抽象类的应用
【4月更文挑战第30天】本文探讨了PHP中接口与抽象类的使用,包括定义、实现和比较。接口用于规定实现类必须提供的方法签名,而抽象类则可以包含方法实现和抽象方法。一个类可实现多个接口,但只能继承一个抽象类。根据需求,若需定义不相关类的共同方法,选择接口;若需提供共享属性和非抽象方法,选择抽象类。通过实战应用示例,展示了如何在动物园管理系统中结合接口和抽象类进行设计。理解两者有助于提升代码的复用性和可维护性。
|
20天前
|
XML JSON API
快速淘宝商品详情页面API接口传输 php
PI(Application Programming Interface,应用程序接口)是一组预定义的函数、协议和工具,用于构建软件应用程序之间的交互。它允许不同的软件系统和应用通过统一的接口进行数据交换和通信
|
20天前
|
PHP
PHP面向对象编程精要:接口、抽象类和继承
PHP面向对象编程涉及接口、抽象类和继承。接口定义了类必须实现的方法,抽象类包含抽象方法,不可实例化,而继承允许子类扩展父类属性和行为。通过案例展示了如何使用interface、abstract和extends关键字。这些概念增强了代码的灵活性、可维护性和可扩展性。
14 1
|
2月前
|
PHP 数据格式
PHP 中的CURL 模拟表单的post提交
PHP 中的CURL 模拟表单的post提交
18 0
|
3月前
|
安全 PHP 数据库
从建站打拿站 -- PHP(GET和POST)
从建站打拿站 -- PHP(GET和POST)
12 0