Get method to submit data
function Http_get ($url, $ssl = FALSE) { $curl = Curl_init ();//Start a Curl session curl_setopt ($curl, Curlopt_url, $url); The address to access if ($ssl) { curl_setopt ($curl, Curlopt_ssl_verifypeer, 0);//Check the source of the certificate curl_setopt ($curl , Curlopt_ssl_verifyhost, 1); Check that the SSL encryption algorithm exists curl_setopt ($curl, Curlopt_sslversion, 3) from the certificate; } curl_setopt ($curl, curlopt_useragent, $_server[' http_user_agent '); Impersonate the user using the browser curl_setopt ($curl, Curlopt_autoreferer, 1);//Auto set Referer curl_setopt ($curl, Curlopt_timeout , 30); Set timeout limit to prevent dead loops curl_setopt ($curl, Curlopt_header, 0);//Displays the contents of the HEADER area returned curl_setopt ($curl, Curlopt_ Returntransfer, 1); The information obtained is returned as a file stream $tmpInfo = curl_exec ($curl);//execute Operation if (Curl_errno ($curl)) { Var_dump (Curl_error ($ Curl)); return FALSE; } Curl_close ($curl); Turn off Curl session return $tmpInfo;//Return Data}
Post mode submit data
function Http_post ($url, $data, $ssl = FALSE) {$curl = Curl_init ();//Start a Curl session curl_setopt ($curl, Curlopt_url, $u RL); The address to access if ($SSL) {curl_setopt ($curl, Curlopt_ssl_verifypeer, 0);//Check the source of the certificate curl_setopt ($curl, C Urlopt_ssl_verifyhost, 1); Check that the SSL encryption algorithm exists curl_setopt ($curl, Curlopt_sslversion, 3) from the certificate; } curl_setopt ($curl, curlopt_useragent, $_server[' http_user_agent '); Simulates user-used browser curl_setopt ($curl, Curlopt_autoreferer, 1); Auto set Referer curl_setopt ($curl, Curlopt_post, 1); Send a regular POST request curl_setopt ($curl, Curlopt_postfields, $data); Post-Submitted packet curl_setopt ($curl, Curlopt_timeout, 30); Set timeout limit to prevent dead loops curl_setopt ($curl, Curlopt_header, 0); Displays the contents of the Returned header area curl_setopt ($curl, Curlopt_returntransfer, 1); The information obtained is returned as a file stream $tmpInfo = curl_exec ($curl); Execute action if (Curl_errno ($curl)) {return FALSE; } curl_close ($curl); Turn off the curl session return $tmpInfo; Return Data}
Application Example ($ssl True to indicate that the requested address is HTTPS, that is, secure HTTP)
echo http_get (' https://www.baidu.com ', TRUE); Get method Request $data = Json_encode (Array ( ' key1 ' = ' value1 ', ' key2 ' = ' value2 ', ' key3 ' = = Array ( ' Key4 ' = ' value4 ', ' key5 ' = ' value5 ')) ; Echo http_post (' https://www.cqh.com ', $data, TRUE); Post mode to submit data
Reference: http://www.blogfshare.com/php-curl-get-post.html
PHP simulates get and post requests using curl