PHP emulates the Post submission request, calling the interface

Source: Internet
Author: User

/**     *  analog post for URL requests      *  @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 ();//Initialize Curl        curl_setopt ($ch,  curlopt_url, $POSTURL);// Crawl a specified Web page         curL_setopt ($ch,  curlopt_header, 0);//Settings Header        curl_ Setopt ($ch,  curlopt_returntransfer, 1);//requires the result to be a string and output to the screen          curl_setopt ($ch,  curlopt_post, 1),//post submission method          curl_setopt ($ch, curlopt_postfields,  $curlPost);         $ Data = curl_exec ($ch);//Run Curl        curl_close ($ch);                 return  $data;     }

This is the method, the following is the specific invocation case.

Function testaction () {         $url  =  '/http mobile.jschina.com.cn/jschina/register.php ';         $post _data[' AppID ' ]       =  ';         $post _ data[' Appkey ']      =  ' Cmbohpffxvr03nipkkqxaaa1vf5no4nq ';          $post _data[' member_name '] =  ' zsjs123 ';          $post _data[' password ']    =  ' 123456 ';          $post _data[' email ']    =  ' [email protected] ';          $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);     }

This submits the request and gets the result of the request. The result of the general return is in JSON format.

The post here is stitched up.

can also be transformed into the following way.

/**     *  analog post for URL requests      *  @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 ();//Initialize curl         curl_setopt ($ch,  curlopt_url, $POSTURL);//crawl specified pages          curl_setopt ($ch,  curlopt_header, 0);//Settings header         curl_setopt ($ch,  curlopt_returntransfer, 1);//requires the result to be a string and output to the screen          curl_setopt ($ch,  curlopt_post, 1),//post submission method          curl_setopt ($ch, curlopt_postfields,  $curlPost);          $data  = curl_EXEC ($ch);//Run Curl        curl_close ($ch);                 return  $data;     }

The

also encapsulates the stitching, which makes the call more concise.

Function testaction () {         $url  =  '/http mobile.jschina.com.cn/jschina/register.php ';         $post _data[' AppID ' ]       =  ';         $post _ data[' Appkey ']      =  ' Cmbohpffxvr03nipkkqxaaa1vf5no4nq ';          $post _data[' member_name '] =  ' zsjs124 ';          $post _data[' password ']    =  ' 123456 ';          $post _data[' email ']    =  ' [email protected] ';         //$post _data = array ();          $res  =  $this->request_post ($url,  $post _data);            &nBsp;   print_r ($res);     } 


PHP emulates the Post submission request, calling the interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.