在PHP中,我在许多PHP项目中都看到了cURL这个词。是什么?它是如何工作的?

参考链接:cURL


当前回答

cURL扩展到PHP的目的是让你从你的PHP脚本中使用各种web资源。

其他回答

cURL是一种从代码中命中URL以获得html响应的方法。cURL的意思是客户端URL,它允许你与其他URL连接,并在你的代码中使用它们的响应。

cURL是一个允许您在PHP中发出HTTP请求的库。您需要了解的关于它(以及大多数其他扩展)的一切都可以在PHP手册中找到。

为了使用PHP的cURL函数 您需要安装»libcurl 包中。PHP要求您使用 Libcurl 7.0.2 beta或更高版本。在PHP中 4.2.3,您需要libcurl版本7.9.0或更高。从PHP 4.3.0开始,您将需要一个libcurl版本 7.9.8或更高。PHP 5.0.0需要libcurl 7.10.5或更高版本。

您也可以在没有cURL的情况下进行HTTP请求,尽管它需要在php.ini文件中启用allow_url_fopen。

// Make a HTTP GET request and print it (requires allow_url_fopen to be enabled)
print file_get_contents('http://www.example.com/');

PHP中的cURL是使用PHP语言中的命令行cURL的桥梁

CURL在PHP中:

简介:

PHP中的curl_exec命令是一个从控制台使用curl的桥梁。curl_exec可以轻松快速地执行GET/POST请求,接收来自其他服务器的响应,如JSON和下载文件。

警告,危险:

curl is evil and dangerous if used improperly because it is all about getting data from out there in the internet. Someone can get between your curl and the other server and inject a rm -rf / into your response, and then why am I dropped to a console and ls -l doesn't even work anymore? Because you mis underestimated the dangerous power of curl. Don't trust anything that comes back from curl to be safe, even if you are talking to your own servers. You could be pulling back malware to relieve fools of their wealth.

例子:

这些都是在Ubuntu 12.10上完成的

Basic curl from the commandline: el@apollo:/home/el$ curl http://i.imgur.com/4rBHtSm.gif > mycat.gif % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 492k 100 492k 0 0 1077k 0 --:--:-- --:--:-- --:--:-- 1240k Then you can open up your gif in firefox: firefox mycat.gif Glorious cats evolving Toxoplasma gondii to cause women to keep cats around and men likewise to keep the women around. cURL example get request to hit google.com, echo to the commandline: This is done through the phpsh terminal: php> $ch = curl_init(); php> curl_setopt($ch, CURLOPT_URL, 'http://www.google.com'); php> curl_exec($ch); Which prints and dumps a mess of condensed html and javascript (from google) to the console. cURL example put the response text into a variable: This is done through the phpsh terminal: php> $ch = curl_init(); php> curl_setopt($ch, CURLOPT_URL, 'http://i.imgur.com/wtQ6yZR.gif'); php> curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); php> $contents = curl_exec($ch); php> echo $contents; The variable now contains the binary which is an animated gif of a cat, possibilities are infinite. Do a curl from within a PHP file: Put this code in a file called myphp.php: <?php $curl_handle=curl_init(); curl_setopt($curl_handle,CURLOPT_URL,'http://www.google.com'); curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2); curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); $buffer = curl_exec($curl_handle); curl_close($curl_handle); if (empty($buffer)){ print "Nothing returned from url.<p>"; } else{ print $buffer; } ?> Then run it via commandline: php < myphp.php You ran myphp.php and executed those commands through the php interpreter and dumped a ton of messy html and javascript to screen. You can do GET and POST requests with curl, all you do is specify the parameters as defined here: Using curl to automate HTTP jobs

危险提示:

小心倾倒卷曲输出,如果其中任何一个被解释和执行,你的盒子被拥有,你的信用卡信息将被卖给第三方,你会从阿拉巴马州的一个人地板公司那里得到900美元的神秘费用,这个公司是海外信用卡欺诈犯罪团伙的前线。

cURL扩展到PHP的目的是让你从你的PHP脚本中使用各种web资源。