PHP Curl methods to implement HTTP and HTTPS requests _php tips

Source: Internet
Author: User

This article describes the Php Curl implementation of HTTP and HTTPS request method, share for everyone to reference. Specifically as follows:

Generally speaking, PHP's Curl function group can help us to take the machine camouflage adult behavior to crawl the website, below to share two examples, one is accesses the HTTP webpage, one accesses the HTTPS webpage, looks together.

Every time you use curl, you always have to look up a bunch of data.
Now will be used to save a few sentences, the province every time to go to Google.

General Curl Request:

Copy Code code as follows:
$url = ' http://www.jb51.net ';
$curl = Curl_init ();
curl_setopt ($curl, Curlopt_url, $url);
curl_setopt ($curl, Curlopt_header, 1);
curl_setopt ($curl, Curlopt_returntransfer, 1);
$data = curl_exec ($curl);
Curl_close ($curl);
Var_dump ($data);

use Curl to request https:

Copy Code code as follows:
$url = ' https://www.jb51.net ';
$curl = Curl_init ();
curl_setopt ($curl, Curlopt_url, $url);
curl_setopt ($curl, Curlopt_header, 1);
curl_setopt ($curl, Curlopt_returntransfer, 1);
curl_setopt ($curl, Curlopt_ssl_verifypeer, false);//This is the point.
$data = curl_exec ($curl);
Curl_close ($curl);
Var_dump ($data);

Attention

When requesting data for HTTPS, a certificate is required, and the following two parameters are added to circumvent the SSL certificate check

Copy Code code as follows:
curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE); HTTPS request does not verify certificate and hosts
curl_setopt ($ch, Curlopt_ssl_verifyhost, FALSE);

I hope this article will help you with your PHP program design.

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.