getting-started with PHP/CURL.

转载 2006年06月04日 09:33:00
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 configurable, 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 many 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 commercial or closed-source.

cURL should not to be confused with the Curl Corporation, which is the commercial 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 external extensions. Once PHP is installed, you will need to copy the files php4ts.dll, ssleay32.dll, php_curl.dll, msvcrt.dll from the 'DLLs' 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 on the openssl library for SSL connections, openssl must be installed first. If openssl is 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, the phpinfo() function will display it in its output.

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

php下扩展php_curl.dll的安装:

已经内置有php_curl.dll,在ext目录下,此DLL用于支持SSL和zlib.
在php.ini中找到有extension=php_curl.dll, 去掉前面的注释.
设置extension_dir=c:/php/ext, 刷新PHP页面时报错, 说找不到模块php_curl.dll.
拷贝php_curl.dll 到windows/system32,还是同样的错.
在网上找了一下,需要将:

libeay32.dll, ssleay32.dll, php5ts.dll, php_curl.dll

都拷贝到system32目录下,重启apache即可.

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

A Simple cURL Example

Using cURL from the command line is extremely easy. The following example retrieves a web page and prints the page to stdout:

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($chCURLOPT_URL,$url); // set url to post to
curl_setopt($chCURLOPT_FAILONERROR1);
curl_setopt($chCURLOPT_FOLLOWLOCATION1);// allow redirects
curl_setopt($chCURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($chCURLOPT_TIMEOUT3); // times out after 4s
curl_setopt($chCURLOPT_POST1); // set POST method
curl_setopt($chCURLOPT_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($chCURLOPT_RETURNTRANSFER1); 
curl_setopt($chCURLOPT_URL$url); 
curl_setopt($chCURLOPT_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($chCURLOPT_URL$url); 
curl_setopt($chCURLOPT_RETURNTRANSFER1); 
curl_setopt($chCURLOPT_UPLOAD1); 
curl_setopt($chCURLOPT_INFILE$fp); 
curl_setopt($chCURLOPT_FTPASCII1); 
curl_setopt($chCURLOPT_INFILESIZEfilesize(__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 decision 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 like fsockopen(). 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 spends developing websites like http://www.fatpigeon.com. Feel free to send any questions or comments to jthome@fcgov.com

php $this到底是什么

一般说法:¥this表示当前所在类对象这个很不明确 $this取决于¥this所在方法被调用时的执行对象(环境) 方法执行环境:当前方法在哪个对象环境下执行,该方法的¥this就表示该对象,针对还原一...
  • xiaobaig0407
  • xiaobaig0407
  • 2015年02月22日 19:07
  • 1536

php断点调试的几种方法

断点调试是程序开发过程中的一个必不可少的步骤,一些程序的断点调试操作比较简单,只需在编辑器里标记哪行为断点,便可得知程序各个环节的输出结果,例如c#、VB、Delphi等语言。 但是对于php程序的...
  • u013939746
  • u013939746
  • 2015年09月09日 17:52
  • 14539

PHP异步编程简述

概述 异步编程,我们从字面上理解,可以理解为代码非同步执行的。异步编程可以归结为四种模式:回调、事件监听、发布/订阅、promise模式。我们最熟悉的两种模式是回调和事件监听,举两个最简单的ja...
  • huyanping
  • huyanping
  • 2015年10月15日 09:56
  • 10586

PHP面向对象基础概念(抽象方法和抽象类)

在OOP语言中,一个类可以有一个或多个子类,而每个类都有至少一个公有方法做为外部代码访问其的接口。而抽象方法就是为了方便继承而引入的,我们先来看一下抽象类和抽象方法的定义再说明它的用途。 什么是抽象方...
  • PHPwhite
  • PHPwhite
  • 2015年06月15日 23:40
  • 2473

PHP架构-PHP内核

http://blog.areawebsolutions.com/php-architecture-the-php-core.html/comment-page-1
  • iefreer
  • iefreer
  • 2013年11月01日 18:58
  • 5157

RabbitMQ官方中文入门教程(PHP版) 第二部分:工作队列(Work queues)

工作队列 在第一篇教程中,我们已经写了一个从已知队列中发送和获取消息的程序。在这篇教程中,我们将创建一个工作队列(Work Queue),它会发送一些耗时的任务给多个工作者(Works )。 ...
  • alexdream
  • alexdream
  • 2015年01月27日 20:33
  • 8716

2016某知名互联网公司PHP面试题及答案

1 字符串”\r”,”\n”,”\t”,”\x20”分别代表什么答案: “\r”代表的含义是: 在Linux、unix 中表示返回到当行的最开始位置,在Mac OS 中表示换行且返回到下一行的最开始...
  • whq19890827
  • whq19890827
  • 2016年09月27日 20:27
  • 35981

php的特殊协议php://

来自:http://www.php.net/manual/zh/wrappers.php.php
  • tenfyguo
  • tenfyguo
  • 2014年06月09日 12:57
  • 5604

php查询数据的4个fetch用法和区别

mysql_fetch_row;mysql_fetch_array;mysql_fetch_assoc;mysql_fetch_object;
  • tuku0801
  • tuku0801
  • 2014年10月02日 12:10
  • 3373

基于PHP的排列组合(如输入a,b,c 输出他们的全部组合)

全部组合
  • feihongxueni
  • feihongxueni
  • 2016年03月24日 18:25
  • 2016
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:getting-started with PHP/CURL.
举报原因:
原因补充:

(最多只允许输入30个字)