/** * 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