Getting-started with PHP/curl.

Source: Internet
Author: User
Tags curl options how to use curl http authentication http post
Intended audience
Overview
Learning Objectives
What are curl and libcurl?
Installing curl
A simple curl example
Using libcurl within PHP
To curl or libcurl?
Summary
Resources
About the author
Intended audience

This tutorial is intended for PHP programmers and web developers interested in using their webserver to transfer files or communicate with other servers. you will need some general knowledge of client-server protocol on the Internet, and a basic knowledge of PHP syntax.
Overview

Curl and libcurl are libaries that allow a webserver to transfer files with a remote computer using a variety of Internet protocols. the libaries are highly retriable, allowing practically any type of client-server request to be peformed. by using these tools, a webserver can act as a client, creating and responding to requests using any technology built on HTTP, like XML-RPC, soap, or WebDAV.
Learning Objectives

In this tutorial you will learn:

  • What curl and libcurl are
  • How to Use curl on the command line
  • How to Use libcurl within PHP
What are curl and libcurl?

Curl stands for "client URLs", and was developed by Daniel Stenberg in 1998 as a command line tool. libcurl is a portable library that provides an easy interface to the curl functionality. it is thread safe, IPv6 compatible, and supports persistent connections. the libcurl PHP binding was added by Sterling Hughes.

Both curl and libcurl can transfer files using a wide variety of protocols, including HTTP, https, FTP, ftps, Gopher, LDAP, dict, telnet and file. the libraries run on practically any * NIX operating system, as well as windows, OS/2, BEOs, and more.

The curl libraries are truly open source, with an MIT/X derivative license. this license is very liberal, allowing the use of curl for whatever you want, commercial or not. you can use libcurl for free, and even include and distribute it with your own application, whether either cial or closed-source.

Curl shoshould not to be confused with the curl Corporation, which is the specified cial producer of the client side programming language, curl.

Installing curl

From PHP version 4.2.3 on, you need a curl version of at least 7.9.0. From PHP version 4.3.0 on, you need a curl version of at least 7.9.8.

Windows:
As with any PHP extension in Windows, you will need the PHP distribution that includes des external extensions. once PHP is installed, you will need to copy the files php4ts. DLL, ssleay32.dll, php_curl.dll, msvcrt. DLL from the 'dls' folder to your windows path, I. e.:

C:/Windows/System for Windows 9x/me
C:/winnt/system32 for Windows NT/2000.
C:/Windows/system32 for Windows XP

Curl can then be enabled by uncommenting the line 'extension = php_curl.dll 'in the PHP. ini file. Alternatively you can load the module dynamically in your script using:

<?php
dl("php_curl.dll");
?>

UNIX:
Your local mirror for downloading curl can be found at http://curl.haxx.se. precompiled binaries are also available for a wide range of operating systems.

Because curl relies onopensslLibrary for SSL connections,opensslMust be installed first. IfopensslIs not installed, SSL support will be omitted from the curl build. After installing curl (./configure, make, make install), PHP must be recompiled to include curl support (--with-curl).

If curl support is enabled,phpinfo()Function will display it in its output.

[-------------------

Install php_curl.dll extension in PHP:

Php_curl.dll has been built in. In the ext directory, This dll is used to support SSL and zlib.
In PHP. ini, find extension = php_curl.dll and remove the preceding comment.
Set extension_dir = C:/PHP/EXT. When the PHP page is refreshed, an error is reported, indicating that the module php_curl.dll cannot be found.
Copy php_curl.dll to Windows/system32.
After searching for it online, you need:

Libeay32.dll, ssleay32.dll, php5ts. dll, php_curl.dll

Copy them to the System32 directory and restart Apache.

----------------]

A simple curl example

Using curl from the command line is extremely easy. The following example retrieves a web page and prints the pagestdout:

$ curl -L zend.com

(The '-l' tells curl to follow redirects .)

Of course, we can execute curl on the command line using PHP. The following example does just that, and gets 3 pages at once:

<?php
$var = echo shell_exec("/usr/bin/curl -L http://www.zend.com http://zend.com/developers.php http://zend.com/zend/tut/");
?>

Using libcurl with PHP

While using curl from within PHP is an option, using the libcurl PHP binding is much easier, especially for things like an http post operation.

The process of using libcurl from within PHP is a matter of following these basic steps:

  • Initialize the curl session
  • Set the curl options (the order of the options is not important)
  • Execute the options in the curl session
  • Close the curl session

Following this process, here are a few examples demonstrating http post, HTTP authentication, and FTP:

<?php
// FIND BOOKS ON PHP AND MYSQL ON AMAZON
$url = "http://www.amazon.com/exec/obidos/search-handle-form/002-5640957-2809605";
$ch = curl_init();    // initialize curl handle
curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 3); // times out after 4s
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, "url=index%3Dbooks&field-keywords=PHP+MYSQL"); // add POST fields
$result = curl_exec($ch); // run the whole process
curl_close($ch); 
echo $result;
?>

<?php
// HTTP authentication
$url = "http://www.example.com/protected/";
$ch = curl_init();    
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword"); 
$result = curl_exec($ch); 
curl_close($ch); 
echo $result;
?>

<?PHP
// FTP this script to a server
$fp = fopen(__FILE__, "r");
$url = "ftp://username:password@mydomain.com:21/path/to/newfile.php";
$ch = curl_init();    
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_UPLOAD, 1); 
curl_setopt($ch, CURLOPT_INFILE, $fp); 
curl_setopt($ch, CURLOPT_FTPASCII, 1); 
curl_setopt($ch, CURLOPT_INFILESIZE, filesize(__FILE__)); 
$result = curl_exec($ch); 
curl_close($ch); 
?>

Tip: If you are having trouble getting libcurl to do what you want, add the following code before closing the curl handle:

<pre> 
<?php 
print_r(curl_getinfo($ch)); 
echo "/n/ncURL error number:" .curl_errno($ch); 
echo "/n/ncURL error:" . curl_error($ch); 
// ...close cURL handle ($ch) below
?>
</pre>

This will force libcurl to report back on what happened on the last transfer, making it easier to troubleshoot.
To curl or to libcurl?

The demo-as to whether to use curl or libcurl depends on the situation. for instance, if I have a cron job running that e-mails me when a file changes on a remote server, or if my ISP doesn't have libcurl support in their PHP install, using curl makes more sense. however, if I have libcurl support in PHP and I am building a PHP application requiring curl functionality, libcurl is the right choice.
Summary

The curl libraries provide a nice interface for file transfers to and from a webserver. They have support for a wide variety of protocols (like https) giving them an edge over built-in PHP functions likefsockopen(). The libraries are thread-safe, IPv6 compatible, and will work with any technology that is built on top of HTTP. whether you are building simple script to fetch a Web page, or a secure payment gateway, leveraging the functions built into curl can save a lot of time.

Resource list

Curl on SourceForge http://curl.sourceforge.net/
Curl's man page-http://curl.sourceforge.net/docs/manpage.html
PHP/curl manual-http://www.zend.com/manual/ref.curl.php

About the author

Whatever spare time Jim has left after working full-time as a web developer, he could DS developing websites like http://www.fatpigeon.com. Feel free to send any questions or comments to jthome@fcgov.com

Related Article

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.