PHP analog Post method call interface Request Code instance

Source: Internet
Author: User
/**     * analog post for URL request     * @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 specified page        curl_setopt ($ch, Curlopt_ header, 0);//Set Header        curl_setopt ($ch, Curlopt_returntransfer, 1);//require the result to be a string and output to the on-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 a specific invocation case.

    function Testaction () {        $url = ' http://mobile.jschina.com.cn/jschina/register.php ';        $post _data[' AppID ']       = ' ten ';        $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);    }

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 request     * @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 page        curl_setopt ($ch, Curlopt_ header, 0);//Set Header        curl_setopt ($ch, Curlopt_returntransfer, 1);//require the result to be a string and output to the on-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;    }

Stitching is also encapsulated, so the call is more concise.

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); }

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.