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