Home > Article > Backend Development > Using curl_init function in PHP (1/4)_PHP tutorial
The code is as follows:
$ch = curl_init();
$c_url = 'http://www.bkjia.com;
$c_url_data = "product_&type=".$type."";
curl_setopt($ch, curlopt_url,$c_url);
curl_setopt($ch, curlopt_post, 1);
curl_setopt($ch, curlopt_returntransfer, true);
curl_setopt($ch, curlopt_postfields, $c_url_data);
echo $result = curl_exec($ch);
curl_close ($ch);
unset($ch);
Using curl in php tutorial
posted on September 14th, 2008 under php
Original (English) address: http://www.phpit.net/article/using-curl-php Copyright Statement: Attribution-Non-Commercial Use-No Derivatives 2.0
Summary:
In this article, we mainly explain the knowledge of php_curl library and teach you how to better use php_curl.
Introduction
You may encounter this problem in your PHP script code: How to get content from other sites? There are several solutions here; the simplest is to use the fopen() function in PHP, but the fopen function does not have enough parameters to use, such as when you want to build a "web crawler" and want to define the client description of the crawler (ie , firefox), obtain content through different request methods, such as post, get; etc. These requirements cannot be achieved with the fopen() function.
In order to solve the problem we raised above, we can use PHP's extension library-curl. This extension library is usually included in the installation package by default. You can use it to obtain the content of other sites or do other things.
Note: These two pieces of code require the support of the php_curl extension library. Check phpinfo(). If curl support is enabled, it means that the curl library is supported.
1. Enable curl library support in PHP under Windows:
Open php.ini and remove the ; sign before extension=php_curl.dll.
2. Enable curl library support in PHP under Linux:
When compiling php, add –with-curl
after ./configure
In this article, we will take a look at how to use the curl library and see its other uses, but next, we will start with the most basic usage
Basic usage:
In the first step, we create a new curl session through the function curl_init(). The code is as follows:
// create a new curl resource
$ch = curl_init();
?>
We have successfully created a curl session. If we need to obtain the content of a URL, the next step is to pass a URL to the curl_setopt() function, code:
// set url and other appropriate options
curl_setopt($ch, curlopt_url, “http://www.google.com/”);
?>