This article mainly and everyone to share the PHP implementation to send HTTP requests, mainly in the form of code and we share, hope to help everyone.
1 Send a GET request
Test Do_get $url = "http://localhost:8081/SpringMVC01/user/getUserInfoByGet/1.html"; $params =array (' name ' = ' Zhang San '); $result =do_get ($url, $params); echo Json_encode ($result); Return
Encapsulated Request Method
function Do_get ($url, $params) { $url = "{$url}?". Http_build_query ($params); $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_customrequest, ' GET '); curl_setopt ($ch, Curlopt_timeout,); curl_setopt ($ch, Curlopt_postfields, $params); $result = curl_exec ($ch); Curl_close ($ch); return $result; }
Accepted end of Java simulation:
@ResponseBody @RequestMapping (value= "/getuserinfobyget/{userid}", Method=requestmethod.get) public Object Getuserinfobyget (@PathVariable ("UserId") of the Integer UserId, @RequestParam ("name") of String name) { Jsonobject User=new Jsonobject (); User.put ("id", userId); User.put ("name", name); return user; }
2 Sending a POST request
Test do_post $url = "http://localhost:8081/SpringMVC01/user/addUser.html"; $params =array (' id ' =>1, ' name ' = ' Zhang San '); $headers =array ( "Content-type:application/json;charset=utf-8", "accept:application/json;charset= Utf-8 " ); JSON serialization $params =json_encode ($params, json_force_object); $result =do_post ($url, $params, $headers); echo Json_encode ($result); Return
Encapsulating the Request method:
function Do_post ($url, $params, $headers) { $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_customrequest, ' POST '); curl_setopt ($ch, Curlopt_postfields, $params); curl_setopt ($ch, Curlopt_httpheader, $headers); curl_setopt ($ch, Curlopt_timeout,); $result = curl_exec ($ch); Curl_close ($ch); return $result; }
The receiver side of the Java simulation:
@ResponseBody @RequestMapping (value= "/adduser", method=requestmethod.post) public Object AddUser (@ Requestbody User user) { System.out.println (user); return user; }
3 sending files to third parties
Send non-binary file streams
$file = $_files["file"]; $upload _file = Array ( ' type ' = = $file [' type '], ' name ' = = $file [' name '], ' file ' = = $file [' Tmp_name '], ); $result =upload_file01 ($url, $params, $upload _file); echo $result;
Method of Encapsulation:
Upload file non-binary file stream function upload_file01 ($url, $params = Array (), $upload _file = False) { $data = array (); $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_customrequest, ' POST '); if ($upload _file) {//Set upload file $file = new \curlfile ($upload _file [' file '], $upload _file [' type '], $upload _file [' Nam E ']); $params [' file '] = $file; } curl_setopt ($ch, Curlopt_postfields, $params); $result = curl_exec ($ch); Curl_close ($ch); $data = Json_decode ($result, true); return $data; }
Java Analog receive End
@RequestMapping (value = "/uploadfile01") @ResponseBody public Object uploadFile01 (httpservletrequest Request, HttpServletResponse response, @RequestParam (value = "File", required = True) Multipartfile file, @ Requestparam map<string,object> params) { try { FileOutputStream fileoutputstream = new FileOutputStream ("d:\\" + file.getoriginalfilename ()); byte[] bytes = File.getbytes (); Bufferedoutputstream stream = new Bufferedoutputstream (fileoutputstream); Stream.Write (bytes); Stream.Close (); Fileoutputstream.close (); } catch (Exception e) { e.printstacktrace (); } System.out.println ("Upload complete"); return params; }
Send binary Stream
Test Upload_file $url = "http://localhost:8081/SpringMVC01/file/uploadFile02.html"; $params =array (' id ' =>1, ' name ' = ' Zhang San '); $file = $_files["file"]; $headers = Array ( "Content-type:application/octet-stream", "Accept:application/json;" ); $result =upload_file02 ($url, $params, $file [' Tmp_name '], $headers); echo $result;
Method of Encapsulation:
Upload file, binary file stream form function upload_file02 ($url, $params, $file _path, $headers) { $ch = Curl_init (); $url = "{$url}?". Http_build_query ($params); Construct URL curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_customrequest, ' POST '); curl_setopt ($ch, Curlopt_httpheader, $headers); curl_setopt ($ch, Curlopt_timeout,); $file _data = "; if ($file _path) {//Set upload file $fh = fopen ($file _path, ' R '); $file _data = fread ($fh, FileSize ($file _path)); Fclose ($FH); } curl_setopt ($ch, Curlopt_postfields, $file _data); $result = curl_exec ($ch); Curl_close ($ch); return $result; }
Java analog receive side:
Upload file, binary file stream form function upload_file02 ($url, $params, $file _path, $headers) { $ch = Curl_init (); $url = "{$url}?". Http_build_query ($params); Construct URL curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_customrequest, ' POST '); curl_setopt ($ch, Curlopt_httpheader, $headers); curl_setopt ($ch, Curlopt_timeout,); $file _data = "; if ($file _path) {//Set upload file $fh = fopen ($file _path, ' R '); $file _data = fread ($fh, FileSize ($file _path)); Fclose ($FH); } curl_setopt ($ch, Curlopt_postfields, $file _data); $result = curl_exec ($ch); Curl_close ($ch); return $result; }
1 Send a GET request
Test Do_get $url = "http://localhost:8081/SpringMVC01/user/getUserInfoByGet/1.html"; $params =array (' name ' = ' Zhang San '); $result =do_get ($url, $params); echo Json_encode ($result); Return
Encapsulated Request Method
function Do_get ($url, $params) { $url = "{$url}?". Http_build_query ($params); $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_customrequest, ' GET '); curl_setopt ($ch, Curlopt_timeout,); curl_setopt ($ch, Curlopt_postfields, $params); $result = curl_exec ($ch); Curl_close ($ch); return $result; }
Accepted end of Java simulation:
@ResponseBody @RequestMapping (value= "/getuserinfobyget/{userid}", Method=requestmethod.get) public Object Getuserinfobyget (@PathVariable ("UserId") of the Integer UserId, @RequestParam ("name") of String name) { Jsonobject User=new Jsonobject (); User.put ("id", userId); User.put ("name", name); return user; }
2 Sending a POST request
Test do_post $url = "http://localhost:8081/SpringMVC01/user/addUser.html"; $params =array (' id ' =>1, ' name ' = ' Zhang San '); $headers =array ( "Content-type:application/json;charset=utf-8", "accept:application/json;charset= Utf-8 " ); JSON serialization $params =json_encode ($params, json_force_object); $result =do_post ($url, $params, $headers); echo Json_encode ($result); Return
Encapsulating the Request method:
function Do_post ($url, $params, $headers) { $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_customrequest, ' POST '); curl_setopt ($ch, Curlopt_postfields, $params); curl_setopt ($ch, Curlopt_httpheader, $headers); curl_setopt ($ch, Curlopt_timeout,); $result = curl_exec ($ch); Curl_close ($ch); return $result; }
The receiver side of the Java simulation:
@ResponseBody @RequestMapping (value= "/adduser", method=requestmethod.post) public Object AddUser (@ Requestbody User user) { System.out.println (user); return user; }
3 sending files to third parties
Send non-binary file streams
$file = $_files["file"]; $upload _file = Array ( ' type ' = = $file [' type '], ' name ' = = $file [' name '], ' file ' = = $file [' Tmp_name '], ); $result =upload_file01 ($url, $params, $upload _file); echo $result;
Method of Encapsulation:
Upload file non-binary file stream function upload_file01 ($url, $params = Array (), $upload _file = False) { $data = array (); $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_customrequest, ' POST '); if ($upload _file) {//Set upload file $file = new \curlfile ($upload _file [' file '], $upload _file [' type '], $upload _file [' Nam E ']); $params [' file '] = $file; } curl_setopt ($ch, Curlopt_postfields, $params); $result = curl_exec ($ch); Curl_close ($ch); $data = Json_decode ($result, true); return $data; }
Java Analog receive End
@RequestMapping (value = "/uploadfile01") @ResponseBody public Object uploadFile01 (httpservletrequest Request, HttpServletResponse response, @RequestParam (value = "File", required = True) Multipartfile file, @ Requestparam map<string,object> params) { try { FileOutputStream fileoutputstream = new FileOutputStream ("d:\\" + file.getoriginalfilename ()); byte[] bytes = File.getbytes (); Bufferedoutputstream stream = new Bufferedoutputstream (fileoutputstream); Stream.Write (bytes); Stream.Close (); Fileoutputstream.close (); } catch (Exception e) { e.printstacktrace (); } System.out.println ("Upload complete"); return params; }
Send binary Stream
Test Upload_file $url = "http://localhost:8081/SpringMVC01/file/uploadFile02.html"; $params =array (' id ' =>1, ' name ' = ' Zhang San '); $file = $_files["file"]; $headers = Array ( "Content-type:application/octet-stream", "Accept:application/json;" ); $result =upload_file02 ($url, $params, $file [' Tmp_name '], $headers); echo $result;
Method of Encapsulation:
Upload file, binary file stream form function upload_file02 ($url, $params, $file _path, $headers) { $ch = Curl_init (); $url = "{$url}?". Http_build_query ($params); Construct URL curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_customrequest, ' POST '); curl_setopt ($ch, Curlopt_httpheader, $headers); curl_setopt ($ch, Curlopt_timeout,); $file _data = "; if ($file _path) {//Set upload file $fh = fopen ($file _path, ' R '); $file _data = fread ($fh, FileSize ($file _path)); Fclose ($FH); } curl_setopt ($ch, Curlopt_postfields, $file _data); $result = curl_exec ($ch); Curl_close ($ch); return $result; }
Java analog receive side:
//upload file, binary file stream form function upload_file02 ($url, $params, $file _path, $headers) { $ch = Curl_init (); $url = "{$url}?". Http_build_query ($params); Construct URL curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_customrequest, ' POST '); curl_setopt ($ch, Curlopt_httpheader, $headers); curl_setopt ($ch, curlopt_timeout, 60); $file _data = "; if ($file _path) {//set upload file $fh = fopen ($file _path, ' R '); $file _data = fread ($fh, FileSize ($file _path)); Fclose ($FH); } curl_setopt ($ch, Curlopt_postfields, $file _data); $result = curl_exec ($ch); Curl_close ($ch); return $result; }