PHP sends POST requests in three ways
[PHP] code
- Class Request {
- Public static function post ($ url, $ post_data = '', $ timeout = 5) {// curl
- $ Ch = curl_init ();
- Curl_setopt ($ ch, CURLOPT_URL, $ url );
- Curl_setopt ($ ch, CURLOPT_POST, 1 );
- If ($ post_data! = ''){
- Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data );
- }
- Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
- Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );
- Curl_setopt ($ ch, CURLOPT_HEADER, false );
- $ File_contents = curl_exec ($ ch );
- Curl_close ($ ch );
- Return $ file_contents;
- }
- Public static function post2 ($ url, $ data) {// file_get_content
-
- $ Postdata = http_build_query (
- $ Data
- );
-
- $ Opts = array ('http' =>
- Array (
- 'Method' => 'post ',
- 'Header' => 'Content-type: application/x-www-form-urlencoded ',
- 'Content' => $ postdata
- )
- );
-
- $ Context = stream_context_create ($ opts );
- $ Result = file_get_contents ($ url, false, $ context );
- Return $ result;
- }
- Public static function post3 ($ host, $ path, $ query, $ others = '') {// fsocket
- $ Post = "POST $ path HTTP/1.1 \ r \ nHost: $ host \ r \ n ";
- $ Post. = "Content-type: application/x-www-form -";
- $ Post. = "urlencoded \ r \ n $ {others }";
- $ Post. = "User-Agent: Mozilla 4.0 \ r \ nContent-length :";
- $ Post. = strlen ($ query). "\ r \ nConnection: close \ r \ n $ query ";
- $ H = fsockopen ($ host, 80 );
- Fwrite ($ h, $ post );
- For ($ a = 0, $ r = '';! $ ;){
- $ B = fread ($ h, 8192 );
- $ R. = $ B;
- $ A = ($ B = '')? 1-0 );
- }
- Fclose ($ h );
- Return $ r;
- }
- }
|
Three types: PHP and POST