PHP uses curl_init to initiate an http request to simulate login

Source: Internet
Author: User
There are two types of http requests: common http request logon and https request logon, next I will give you a detailed introduction of using curl_init to implement http and h

There are two types of http requests: common http request logon and https request logon, next I will give you a detailed description of using curl_init to achieve http and https login.

Note: to use the curl_init function, you must enable this php extension.

1. open php. ini and enable extension = php_curl.dll.

2. check php. which Directory is the extension_dir value of ini? check whether php_curl.dll exists. if not, download php_curl.dll and copy libeay32.dll and ssleay32.dll in the php directory to c:/windows/system32.

The code for initiating an http request is as follows:

  1. Function _ http_curl_post ($ url, $ data)
  2. {
  3. $ Ch = curl_init ();
  4. Curl_setopt ($ ch, CURLOPT_URL, $ url );
  5. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
  6. Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, true );
  7. Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 4 );
  8. Curl_setopt ($ ch, CURLOPT_TIMEOUT, 4 );
  9. If ($ data ){
  10. Curl_setopt ($ ch, CURLOPT_POST, 1 );
  11. Curl_setopt ($ ch, CURLOPT_POSTFIELDS, "value =". json_encode ($ data); // Convert request parameters to json format
  12. }
  13. Curl_setopt ($ ch, CURLOPT_HEADER, false );
  14. $ String = curl_exec ($ ch );
  15. Curl_close ($ ch );
  16. Return $ string;
  17. }

The code is as follows:

  1. $ Params = array ();
  2. $ Params ['id'] = 1
  3. $ Params ['web _ name'] = 'good script ';
  4. $ Params ['web _ url'] = 'http: // www.phpfensi.com /';
  5. $ Params ['web _ miaoshu '] = 'script programming example ';
  6. $ Data = _ curl_post ($ url, $ params );
  7. $ Arr = json_decode ($ data );

In addition to http requests, there is also an https request. The last time I log on to Renren, the interface is the https url. Using the above function, an error is reported, if you encounter such a problem, you can refer to the following methods to solve it.

Https request example code:

  1. Function _ https_curl_post ($ url, $ vars)
  2. {
  3. Foreach ($ vars as $ key => $ value)
  4. {
  5. $ Fields_string. = $ key. '='. $ value .'&';
  6. }
  7. $ Fields_string = substr ($ fields_string, 0, (strlen ($ fields_string)-1 ));
  8. $ Ch = curl_init ();
  9. Curl_setopt ($ ch, CURLOPT_URL, $ url );
  10. Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, 2 );
  11. Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE); // this line makes it work under https
  12. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
  13. Curl_setopt ($ ch, CURLOPT_POST, count ($ vars ));
  14. Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields_string );
  15. $ Data = curl_exec ($ ch );
  16. Curl_close ($ ch );
  17. If ($ data)
  18. {
  19. Return $ data;
  20. }
  21. Else
  22. {
  23. Return false;
  24. }
  25. }

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.