PHP sends a POST request

Source: Internet
Author: User
  1. /**
  2. * Send POST request
  3. * @param string $url request address
  4. * @param array $post _data post key value pair data
  5. * @return String
  6. */
  7. function send_post ($url, $post _data) {
  8. $postdata = http_build_query ($post _data);
  9. $options = array(
  10. ' http ' = = Array(
  11. ' method ' = ' POST ',
  12. ' header ' = ' content-type:application/x-www-form-urlencoded ',
  13. ' content ' = $postdata,
  14. ' timeout ' = * + //Time-out (unit: s)
  15. )
  16. );
  17. $context = stream_context_create ($options);
  18. $result = file_get_contents($url, False, $context);
  19. return $result;
  20. }
  21. //How to use
  22. $post _data = array(
  23. ' username ' = ' stclair2201 ',
  24. ' password ' = ' Handan '
  25. );
  26. Send_post (' http://www.qianyunlai.com ', $post _data);
  27. /**
  28. * Socket version
  29. * How to use:
  30. * $post _string = "App=socket&version=beta";
  31. * Request_by_socket (' chajia8.com ', '/restserver.php ', $post _string);
  32. */
  33. function request_by_socket ($remote _server,$remote _path,$post _string,$ Port = up,$timeout = 30) {
  34. $socket = fsockopen($remote _server, $port, $errno, $errstr , $timeout);
  35. if (! $socket) die ("$errstr ($errno)");
  36. Fwrite ($socket, "POST $remote _path http/1.0");
  37. Fwrite ($socket, "User-agent:socket Example");
  38. Fwrite ($socket, "HOST: $remote _server");
  39. Fwrite ($socket, "content-type:application/x-www-form-urlencoded");
  40. Fwrite ($socket, "content-length:" .) (strlen($post _string) + 8). "");
  41. Fwrite ($socket, "accept:*/*");
  42. Fwrite ($socket, "");
  43. Fwrite ($socket, "mypost= $post _string");
  44. Fwrite ($socket, "");
  45. $header = "";
  46. while ($str = Trim (fgets($socket, 4096)) {
  47. $header . = $str;
  48. }
  49. $data = "";
  50. while (! feof ($socket)) {
  51. $data . = fgets($socket, 4096);
  52. }
  53. return $data;
  54. }
  55. ?>
  56. /**
  57. * Curl Version
  58. * How to use:
  59. * $post _string = "App=request&version=beta";
  60. * Request_by_curl (' http://www.qianyunlai.com/restServer.php ', $post _string);
  61. */
  62. function request_by_curl ($remote _server, $post _string) {
  63. $ch = Curl_init ();
  64. curl_setopt ($ch, Curlopt_url, $remote _server);
  65. curl_setopt ($ch, Curlopt_postfields, ' mypost= ' . $post _string);
  66. curl_setopt ($ch, Curlopt_returntransfer, true);
  67. curl_setopt ($ch, curlopt_useragent, "qianyunlai.com's CURL Example beta");
  68. $data = curl_exec ($ch);
  69. Curl_close ($ch);
  70. return $data;
  71. }
  72. ?>

Original address: http://blog.sjzycxx.cn/post/435/

The above describes the PHP send POST request, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.