PHP three ways to send post, get requests

Source: Internet
Author: User
Tags vars

Send POST request//@param string $url request address//@param array $post _data post key value pair data//@return string function Sendpos T ($url, $post _data) {//Http_build_query ()///Generate Url-encode after request string////Remarks:///        The delimiter for the php5.3 is & if the target server is also php5.3, then there will be no error.        But if the target server is Java tomcat or something else, then & may handle the error.                The following forms can avoid errors//Http_build_query ($post _data, ', ' & '); Stream_context_create ()//Create and return a resource for a stream$username = ' username ';$password = ' password ';$postData = Http_build_query ($post _data, ' & '); $options = Array (' http ' =>array (' method ' = ' "POST", ' header ' = ') ' Accept-la                          Nguage:en\r\n ".                          "Cookie:foo=bar\r\n". "Content-type:application/x-www-form-urlencoded\r\n"."Authorization:basic". Base64_encode ("$username: $password"). ' \ r \ n ', ' content ' = $postData, ' timeout ' = 15 * 60,//timeout (in s)));        Creates and returns a resource for a stream $context = Stream_context_create ($options);         $result = File_get_contents ($url, False, $context);    return $result; Note: If the destination address requires login verification, you will need toRed WordPart of the code, if no login verification is required,Red WordPart of the code is not written
  1. <?php
  2. /**
  3. * Socket version
  4. * How to use:
  5. * $post _string = "App=socket&version=beta";
  6. * Request_by_socket (' chajia8.com ', '/restserver.php ', $post _string);
  7. */
  8. function Request_by_socket ($remote _server,$remote _path,$post _string,$port =,$timeout = 30) { /c8>
  9. $socket = fsockopen ($remote _server, $port, $errno, $errstr, $timeout);
  10. if (!   $socket) die ("$errstr ($errno)");
  11. Fwrite ($socket, "POST $remote _path http/1.0");
  12. Fwrite ($socket, "User-agent:socket Example");
  13. Fwrite ($socket, "HOST: $remote _server");
  14. Fwrite ($socket, "content-type:application/x-www-form-urlencoded");
  15. Fwrite ($socket, "Content-length:".) (strlen ($post _string) + 8).  "");
  16. Fwrite ($socket, "accept:*/*");
  17. Fwrite ($socket, "");
  18. Fwrite ($socket, "mypost= $post _string");
  19. Fwrite ($socket, "");
  20. $header = "";
  21. While ($str = Trim (fgets ($socket, 4096))) {
  22. $header. = $str;
  23. }
  24. $data = "";
  25. While (! Feof ($socket)) {
  26. $data. = fgets ($socket, 4096);
  27. }
  28. return $data;
  29. }
  30. ?>
  31. <?php
  32. /**
  33. * Curl Version
  34. * How to use:
  35. * $post _string = "App=request&version=beta";
  36. * Request_by_curl (' http://www.qianyunlai.com/restServer.php ', $post _string);
  37. */
  38. function Request_by_curl ($remote _server, $post _string) {
  39. $ch = Curl_init ();
  40. curl_setopt ($ch, Curlopt_url, $remote _server);
  41. curl_setopt ($ch, Curlopt_postfields, ' mypost= '.  $post _string);
  42. curl_setopt ($ch, Curlopt_returntransfer, true);
  43. curl_setopt ($ch, curlopt_useragent, "qianyunlai.com's CURL Example beta");
  44. $data = curl_exec ($ch);
  45. Curl_close ($ch);
  46. return $data;
  47. }
  48. ?>
 

PHP three ways to send post, get requests

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.