PHP using the socket, curl, file_get_contents method Post data instance

Source: Internet
Author: User
  1. /**
  2. * Socket version
  3. * How to use:
  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 = up, $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. * How to use:
  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. * How to use:
  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. ?>
Copy Code
  • Related Article

    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.