PHP's curl implements Get,post and cookies (several instances)

Source: Internet
Author: User
Tags set cookie vars

Similar to dreamhost such as host service providers, is to display the use of fopen. Use of PHP's curl can be implemented to support FTP, FTPS, HTTP htpps SCP SFTP TFTP, TELNET DICT file and LDAP. Curl supports SSL certificates, HTTP POST, http PUT, FTP uploads, Kerberos, HTT-based uploads, proxies, cookies, user + password proofs, File transfer recovery, HTTP proxy channels are most commonly used, HTTP-based Get and Post methods.

Code implementation:

1. Get implementation of HTTP

  1. $ch = Curl_init ("http://www.domain.com/api/index.php?test=1");
  2. curl_setopt ($ch, Curlopt_returntransfer, true); //Get Data back
  3. curl_setopt ($ch, Curlopt_binarytransfer, true); //Get Data back when Curlopt_returntransfer is enabled
  4. echo $output = curl_exec ($ch);
  5. /* Write file */
  6. $fh = fopen ("out.html", ' W ');
  7. Fwrite ($fh, $output);
  8. Fclose ($fh);

2, the HTTP POST implementation

PHP code
  1. <?php
  2. $url = ' http://www.domain.com/api/';
  3. $fields = Array (
  4. ' lname ' = 'justcoding ',
  5. ' fname ' = 'phplover ',
  6. ' title ' = 'myapi ',
  7. ' age ' = ',
  8. ' email ' = '[email protected] ',
  9. ' phone ' = '1353777303 '
  10. );
  11. $post _data = Implode (' & ', $fields);
  12. Open connection
  13. $ch = Curl_init ();
  14. Set the URL, number of post VARs, post data
  15. curl_setopt ($ch, Curlopt_url,$url);
  16. curl_setopt ($ch, Curlopt_post,count ($fields));  //When enabled, a regular post request is sent with the type: application/x-www-form-urlencoded, just like the form submitted.
  17. curl_setopt ($ch, Curlopt_postfields,$fields); //"POST" operation in HTTP. If you want to transfer a file, you need a filename at the start of the @
  18. Ob_start ();
  19. Curl_exec ($ch);
  20. $result = Ob_get_contents ();
  21. Ob_end_clean ();
  22. echo $result;
  23. Close connection
  24. Curl_close ($ch);

http://www.domain.com/api/index.php

PHP code
    1. <?php
    2. if ($_get[' test ')
    3. {
    4. Print_r ($_get);
    5. }
    6. if ($_post)
    7. {
    8. Print_r ($_post);
    9. }

3. PHP's Curl Transfer cookie

Two different ways:

One is automatic:

PHP code
    1. curl_setopt ($curlHandle, Curlopt_cookiejar, ' cookie.txt '); //Save
    2. curl_setopt ($curlHandle, Curlopt_cookiefile, ' cookie.txt '); //Read

This will automatically keep the cookie up.
But two times, one is to first access the cookie, then the link to use the cookie

Example:

PHP code
  1. <?php
  2. function Get_curlcuconent2 ($filename,$referer)
  3. {
  4. $cookie _jar = Tempnam ('./tmp ',' jsessionid ');
  5. $ch = Curl_init ();
  6. curl_setopt ($ch, Curlopt_url, $filename);
  7. curl_setopt ($ch, Curlopt_header, false);
  8. curl_setopt ($ch, Curlopt_returntransfer, 1);
  9. //Set cookie path for file read and submit
  10. curl_setopt ($ch, Curlopt_cookiejar, $cookie _jar);
  11. $filecontent =curl_exec ($ch);
  12. Curl_close ($ch);
  13. $ch = Curl_init ();
  14. $hostname ="www.domain.com";
  15. //$referer = "http://www.domain.com/";
  16. curl_setopt ($ch, Curlopt_url, $filename);
  17. curl_setopt ($ch, Curlopt_referer, $referer); //See here, you can also say you come from Google
  18. curl_setopt ($ch, curlopt_useragent, "www.domain.com");
  19. //$request = "jsessionid=abc6szw15ozvz_pu9b-8r";//Set Post parameters
  20. //curl_setopt ($ch, Curlopt_postfields, $request);
  21. //Above this sentence, of course you can say you are Baidu, get rid of the value here OK, can realize the function of thieves, $_server[' http_user_agent ']
  22. //You can also make yourself a spider, so disguise the curlopt_useragent here .
  23. //If you want to put this program on Linux with Php-q execution that also write specific $_server[' http_user_agent ', forged can also
  24. curl_setopt ($ch, Curlopt_returntransfer, 1);
  25. curl_setopt ($ch, Curlopt_cookiefile, $cookie _jar);
  26. curl_setopt ($ch, Curlopt_header, false); Set whether to output page content
  27. curl_setopt ($ch, Curlopt_get, 1); //Post,get past
  28. $filecontent = curl_exec ($ch);
  29. Preg_match_all ("/charset= (. +?)  [null\ "\ ']/is",$filecontent, $charsetarray);
  30. if (strtolower ($charsetarray [1][0]) = ="Utf-8")
  31. $filecontent =iconv ( ' utf-8 ', ' Gb18030//ignore ', $filecontent);
  32. Curl_close ($ch);
  33. return $filecontent;
  34. }
  35. ?>


A custom:

PHP code
  1. $header []= ' Accept:image/gif, Image/x-xbitmap, Image/jpeg, Image/pjpeg, Application/x-shockwave-flash, text/html , * ‘.  '/* ';
  2. $header []= ' ACCEPT-LANGUAGE:ZH-CN ';
  3. $header []= ' user-agent:mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;  SV1;. NET CLR 2.0.50727) ';
  4. $header []= ' Host: '. $ your target host;
  5. $header []= ' connection:keep-alive ';
  6. $header []= ' Cookie: '. $ your cookie string;
  7. curl_setopt ($curlHandel, Curlopt_httpheader,$header);

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.