Php uses the socket, curl, file_get_contents method to POST data instances

Source: Internet
Author: User
Php uses the socket, curl, file_get_contents method to POST data instances

  1. /**
  2. * Socket version
  3. * Usage:
  4. * $ Post_string = "app = socket & version = beta ";
  5. * Request_by_socket ('www .1bo8.cn ','/restServer. php', $ post_string );
  6. */
  7. Function request_by_socket ($ remote_server, $ remote_path, $ post_string, $ port = 80, $ timeout = 30 ){
  8. $ Socket = fsockopen ($ remote_server, $ port, $ errno, $ errstr, $ timeout );
  9. If (! $ Socket) die ("$ errstr ($ errno )");
  10. Fwrite ($ socket, "POST $ remote_path HTTP/1.0 \ r \ n ");
  11. Fwrite ($ socket, "User-Agent: Socket Example \ r \ n ");
  12. Fwrite ($ socket, "HOST: $ remote_server \ r \ n ");
  13. Fwrite ($ socket, "Content-type: application/x-www-form-urlencoded \ r \ n ");
  14. Fwrite ($ socket, "Content-length:". strlen ($ post_string) + 8. "\ r \ n ");
  15. Fwrite ($ socket, "Accept: */* \ r \ n ");
  16. Fwrite ($ socket, "\ r \ n ");
  17. Fwrite ($ socket, "mypost = $ post_string \ r \ n ");
  18. Fwrite ($ socket, "\ r \ n ");
  19. $ Header = "";
  20. While ($ str = trim (fgets ($ socket, 4096 ))){
  21. $ Header. = $ str;
  22. }
  23. $ Data = "";
  24. While (! Feof ($ socket )){
  25. $ Data. = fgets ($ socket, 4096 );
  26. }
  27. Return $ data;
  28. }
  29. /**
  30. * Curl version
  31. * Usage:
  32. * $ Post_string = "app = request & version = beta ";
  33. * Request_by_curl ('http: // www.1bo8.cn/restserver.php', $post_string );
  34. */
  35. Function request_by_curl ($ remote_server, $ post_string ){
  36. $ Ch = curl_init ();
  37. Curl_setopt ($ ch, CURLOPT_URL, $ remote_server );
  38. Curl_setopt ($ ch, CURLOPT_POSTFIELDS, 'mypost = '. $ post_string );
  39. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
  40. Curl_setopt ($ ch, CURLOPT_USERAGENT, "Jimmy's CURL Example beta ");
  41. $ Data = curl_exec ($ ch );
  42. Curl_close ($ ch );
  43. Return $ data;
  44. }
  45. /**
  46. * Other versions
  47. * Usage:
  48. * $ Post_string = "app = request & version = beta ";
  49. * Request_by_other ('http: // www.1bo8.cn/restserver.php', $post_string );
  50. */
  51. Function request_by_other ($ remote_server, $ post_string ){
  52. $ Context = array (
  53. 'Http' => array (
  54. 'Method' => 'post ',
  55. 'Header' => 'Content-type: application/x-www-form-urlencoded'. "\ r \ n ".
  56. 'User-Agent: Jimmy \'s POST Example beta '. "\ r \ n ".
  57. 'Content-length: '. strlen ($ post_string) + 8,
  58. 'Content' => 'mypost = '. $ post_string)
  59. );
  60. $ Stream_context = stream_context_create ($ context );
  61. $ Data = file_get_contents ($ remote_server, FALSE, $ stream_context );
  62. Return $ data;
  63. }
  64. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.