最新文章:

首页 PHP

PHP接口开发

发布时间:2015年07月16日 评论数:抢沙发 阅读数:3572

    1.get方法curl传输方式发送json格式的数据到localhost/test/interface/get_json.php接口,接收接口返回的json格式数据
    $json = array(
        'name' => 'faye',
        'age' => 20
    );
    $my_json = json_encode($json);

    $url = "localhost/test/interface/get_json.php?json=".$my_json;
            // 1. 初始化
            $ch = curl_init();
            // 2. 设置选项,包括URL
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            // 3. 执行并获取HTML文档内容
            $output = curl_exec($ch);
            // 4. 释放curl句柄
            curl_close($ch);
            $output = json_decode($output);
            var_dump($output);
            //echo $output;

    2.php接口接收json格式的数据,并且返回json格式的数据
    $json = isset($_GET['json']) ? trim($_GET['json']) : 0;
    $my_json = json_decode($json,true);
    $return = array();
    foreach ($my_json as $key => $val)
    {
        $return[] = $val;
    }
    echo json_encode($return);

    3.php用get的方式curl的方法传输普通格式数据到接口localhost/test/interface/get.php


    4.php 用get方法接收普通格式数据
    $get = isset($_GET['name']) ? trim($_GET['name']) : '';
    ///$get = file_get_contents("php://input");
    echo 'getname = '.$get;

    5.php用file_get_contents()方法传输xml格式数据
    $xml = '<?xml version="1.0" encoding=" GBK" ?> 
    <xmlMobile>
        <FUNCODE>S00004</FUNCODE>
        <MID>1000016453721890</MID>
          <MERID>000000</MERID>
          <ORDERID>20120513405231956793</ORDERID>
          <SUBMERID>20120513405231956793</SUBMERID>
          <SUBORDERID>20120513405231956793</SUBORDERID>
          <ORGORDERDATE>20120513405231956793</ORGORDERDATE>
          <REFUNDAMOUNT>15103672424</REFUNDAMOUNT>
          <AMOUNT>15103672424</AMOUNT>
        <VERSION>1.0</ VERSION >
        <SIGN> ef/4747XdgRIbWHQi/SXbHMxqdwmRlvj8qrCfe/Z0bFVvg2HujJQbci6+DFA7/Yh03c9Dq6TEw0MQrZhJZ9Ql7lrCFg08LhUzyvopr3uaW2VaMpEKz011mP8gh7L9zFybBF+HJTYrRf4A3F2eJWoTDa5q9xINfo932Ok0xxFets=</SIGN>
    </xmlMobile>';
    $xml = urlencode($xml);
    echo file_get_contents("http://localhost/test/interface/get.php?name=".$xml);
    一、get
       一)、发送
            1)、curl
                1.普通
               $name = 'faye';
               $url = 'http://localhost/test/interface/get.php?name='.$name;
               //初始化
               $ch = curl_init();
               //设置选项
          curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    //执行并获取html文档内容
    $output = curl_exec($ch);
    curl_close($ch);
    echo $output;

                2.xml


                3.json
            2)、file_get_contents()
                1.普通
                2.xml
                3.json
       二)、接收
            1)、curl
                1.普通
                2.xml
                3.json
            2)、file_get_contents()
                1.普通
                2.xml
                3.json

二维码加载中...
本文作者:ThinkMany      文章标题: PHP接口开发
本文地址:http://www.thinkmany.com/post/11
版权声明:若无注明,本文皆为“乌龟's Blog”原创,转载请保留文章出处。
挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
提交评论

清空信息
关闭评论
    178.84ms