Ïê½âphpÓÃcurlµ÷Óýӿڷ½·¨£¬getºÍpostÁ½ÖÖ·½Ê½

6年以前  |  阅读数:1002 次  |  编程语言:PHP 

Ê×ÏÈÊǿͻ§¶ËÖ´Ðн¨ApiModel.php£º


    <?php 
    /**
       * Ä£Äapost½øÐÐurlÇeÇo
       * @param string $url
       * @param array $post_data
       */
      function request_post($url = '',$ispost=true, $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);
        $key=md5(base64_encode($post_data));
        if($ispost){
          $url=$url;
        }else{
          $url = $url.'?'.$post_data;
        }


        $curlPost = 'key='.$key;
        header("Content-type: text/html; charset=utf-8");
        $ch = curl_init();//³oʼ»¯curl
        curl_setopt($ch, CURLOPT_URL,$url);//ץȡָ¶¨ÍøÒ³
        curl_setopt($ch, CURLOPT_HEADER, 0);//ÉeÖÃheader
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//ÒªÇo½a¹uΪ×Ö*u´®ÇÒÊa³oµ½ÆÁÄ»ÉÏ
        if($ispost){
          curl_setopt($ch, CURLOPT_POST, 1);//postÌa½»*½Ê½
          curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        }
        $data = curl_exec($ch);//ÔËÐÐcurl
        curl_close($ch);
        return $data;
      }
      ?>

¿Í»§¶Ëµ÷Óý¨£¬¿ÉÒÔÔÚ´ËÅaÖûu±¾ÐÅÏ¢api.php£º


    <?php 
    require 'ApiModel.php';
    function testAction(){
        $url = '½Ó¿ÚµØÖ*';
        $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 = request_post($url,$ispost=true,$post_data);    
        print_r($res);

      }
    testAction();
    ?>

*þÎñÆ÷µÄ½Ó¿Úº¯Êýtest.php£º


    <?php 
    function serverapi(){
      $key='57173d6ad842d807443ee0db91fed323';
      if($_GET&&$_GET['appkey']=='cmbohpffXVR03nIpkkQXaAA1Vf5nO4nQ'||$_POST&&$_POST['key']===$key){
        $arr=array('name'=>'huanglu','password'=>'123456');
        echo json_encode($arr);
      }else{
        exit('*Ç*¨*ÃÎÊ£¡');
      }
    }
    serverapi();
    ?>

ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íu¶Ô´o¼ÒµÄѧϰÓÐËu°iÖu£¬Ò²Ï£Íu´o¼Ò¶a¶aÖ§³Ö½Å±¾Ö®¼Ò¡£

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8