Joh*_*set 235
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开始,您需要一个7.9.8或更高版本的libcurl版本.PHP 5.0.0需要libcurl版本7.10.5或更高版本.
您也可以在没有cURL的情况下发出HTTP请求,但需要allow_url_fopen在php.ini文件中启用它.
// Make a HTTP GET request and print it (requires allow_url_fopen to be enabled)
print file_get_contents('http://www.example.com/');
Run Code Online (Sandbox Code Playgroud)
sus*_*ani 150
cURL是一种可以从代码中获取URL以从中获取html响应的方法.cURL表示客户端URL,允许您与其他URL连接并在代码中使用其响应.
Eri*_*ski 65
摘要:
curl_execPHP中的命令是curl从控制台使用的桥梁.curl_exec可以轻松快速轻松地执行GET/POST请求,接收来自其他服务器(如JSON)和下载文件的响应.
警告,危险:
curl如果使用不当,则是邪恶和危险的,因为它只是从互联网上获取数据.有人可以rm -rf /介入你的curl和其他服务器并注入你的响应,然后为什么我会掉到控制台,ls -l甚至不再工作了?因为你错误地低估了卷曲的危险力量.即使您正在与自己的服务器通信,也不要相信从卷曲中回来的任何东西都是安全的.你可能会撤回恶意软件以减轻他们财富的愚蠢.
这些是在Ubuntu 12.10上完成的
命令行的基本卷曲:
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
Run Code Online (Sandbox Code Playgroud)
然后你可以在firefox中打开你的gif:
firefox mycat.gif
Run Code Online (Sandbox Code Playgroud)
光荣的猫进化弓形虫使妇女养猫,男人同样保持妇女周围.
cURL示例获取请求到google.com,回显到命令行:
这是通过phpsh终端完成的:
php> $ch = curl_init();
php> curl_setopt($ch, CURLOPT_URL, 'http://www.google.com');
php> curl_exec($ch);
Run Code Online (Sandbox Code Playgroud)
其中打印并将一堆浓缩的html和javascript(从谷歌)转储到控制台.
cURL示例将响应文本放入变量中:
这是通过phpsh终端完成的:
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;
Run Code Online (Sandbox Code Playgroud)
变量现在包含二进制文件,它是猫的动画GIF,可能性是无限的.
从PHP文件中做一个卷曲:
将此代码放在名为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;
}
?>
Run Code Online (Sandbox Code Playgroud)
然后通过命令行运行它:
php < myphp.php
Run Code Online (Sandbox Code Playgroud)
你运行myphp.php并通过php解释器执行这些命令,并将大量凌乱的html和javascript转储到屏幕上.
您可以使用curl 执行GET和POST请求,您只需指定此处定义的参数:http://curl.haxx.se/docs/httpscripting.html#POST
提醒危险:
小心倾倒卷曲输出,如果任何一个被解释和执行,你的盒子是拥有的,你的信用卡信息将出售给第三方,你将得到一个神秘的900美元从阿拉巴马州单人地板公司收取的费用是一个前海外信用卡诈骗犯罪集团.
小智 22
cURL是一种可以从代码中获取URL以从中获取HTML响应的方法.它用于PHP语言的命令行cURL.
<?php
// Step 1
$cSession = curl_init();
// Step 2
curl_setopt($cSession,CURLOPT_URL,"http://www.google.com/search?q=curl");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
// Step 3
$result=curl_exec($cSession);
// Step 4
curl_close($cSession);
// Step 5
echo $result;
?>
Run Code Online (Sandbox Code Playgroud)
第1步:使用初始化卷曲会话curl_init().
第2步:设置选项CURLOPT_URL.此值是我们向其发送请求的URL.curl使用参数附加搜索词q=.设置选项CURLOPT_RETURNTRANSFER.True会告诉curl返回字符串而不是打印出来.设置选项为CURLOPT_HEADER,false将告诉curl忽略返回值中的标头.
第3步:使用执行curl会话curl_exec().
第4步:关闭我们创建的卷曲会话.
第5步:输出返回字符串.
public function curlCall($apiurl, $auth, $rflag)
{
$ch = curl_init($apiurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if($auth == 'auth') {
curl_setopt($ch, CURLOPT_USERPWD, "passw:passw");
} else {
curl_setopt($ch, CURLOPT_USERPWD, "ss:ss1");
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$dt = curl_exec($ch);
curl_close($ch);
if($rflag != 1) {
$dt = json_decode($dt,true);
}
return $dt;
}
Run Code Online (Sandbox Code Playgroud)
这也用于身份验证.我们还可以设置用于身份验证的用户名和密码.
有关更多功能,请参阅用户手册或以下教程:
http://php.net/manual/en/ref.curl.php
http://www.startutorial.com/articles/view/php-curl
Ana*_*dey 15
首先让我们理解curl,libcurl和PHP/cURL的概念.
curl:用于使用URL语法获取或发送文件的命令行工具.
libcurl:由Daniel Stenberg创建的库,允许您使用许多不同类型的协议连接和通信到许多不同类型的服务器.libcurl目前支持http,https,ftp,gopher,telnet,dict,file和ldap协议.libcurl还支持HTTPS证书,HTTP POST,HTTP PUT,FTP上传(这也可以使用PHP的ftp扩展),基于HTTP表单的上传,代理,cookie和用户+密码验证.
PHP/cURL:PHP的模块,使PHP程序可以使用libcurl.
如何使用它:
step1:使用curl_init()初始化卷曲会话.
step2:设置CURLOPT_URL的选项.这个值是我们发送请求的URL.使用参数"q ="添加搜索词"curl".设置选项CURLOPT_RETURNTRANSFER,true将告诉curl返回字符串而不是将其打印出来.为CURLOPT_HEADER设置选项,false将告诉curl忽略返回值中的标题.
step3:使用curl_exec()执行curl会话.
step4:关闭我们创建的curl会话.
step5:输出返回字符串.
制作演示:
您需要创建两个PHP文件并将它们放入Web服务器可以从中提供PHP文件的文件夹中.在我的情况下,我将它们放入/ var/www /中以简化.
1. helloservice.php和2. demo.php
helloservice.php非常简单,基本上只是回传它得到的任何数据:
<?php
// Here is the data we will be sending to the service
$some_data = array(
'message' => 'Hello World',
'name' => 'Anand'
);
$curl = curl_init();
// You can also set the URL you want to communicate with by doing this:
// $curl = curl_init('http://localhost/echoservice');
// We POST the data
curl_setopt($curl, CURLOPT_POST, 1);
// Set the url path we want to call
curl_setopt($curl, CURLOPT_URL, 'http://localhost/demo.php');
// Make it so the data coming back is put into a string
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Insert the data
curl_setopt($curl, CURLOPT_POSTFIELDS, $some_data);
// You can also bunch the above commands into an array if you choose using: curl_setopt_array
// Send the request
$result = curl_exec($curl);
// Get some cURL session information back
$info = curl_getinfo($curl);
echo 'content type: ' . $info['content_type'] . '<br />';
echo 'http code: ' . $info['http_code'] . '<br />';
// Free up the resources $curl is using
curl_close($curl);
echo $result;
?>
Run Code Online (Sandbox Code Playgroud)
2.demo.php页面,你可以看到结果:
<?php
print_r($_POST);
//content type: text/html; charset=UTF-8
//http code: 200
//Array ( [message] => Hello World [name] => Anand )
?>
Run Code Online (Sandbox Code Playgroud)
PHP支持libcurl,这是一个由Daniel Stenberg创建的库,它允许您使用许多不同类型的协议连接和通信许多不同类型的服务器.libcurl目前支持http,https,ftp,gopher,telnet,dict,file和ldap协议.libcurl还支持HTTPS证书,HTTP POST,HTTP PUT,FTP上传(这也可以使用PHP的ftp扩展),基于HTTP表单的上传,代理,cookie和用户+密码验证.
一旦使用cURL支持编译PHP,就可以开始使用cURL函数了.cURL函数背后的基本思想是使用curl_init()初始化cURL会话,然后你可以通过curl_setopt()设置所有传输选项,然后你可以用curl_exec()执行会话然后你使用curl_close()完成会话.
// error reporting
error_reporting(E_ALL);
ini_set("display_errors", 1);
//setting url
$url = 'http://example.com/api';
//data
$data = array("message" => "Hello World!!!");
try {
$ch = curl_init($url);
$data_string = json_encode($data);
if (FALSE === $ch)
throw new Exception('failed to initialize');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$output = curl_exec($ch);
if (FALSE === $output)
throw new Exception(curl_error($ch), curl_errno($ch));
// ...process $output now
} catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
365040 次 |
| 最近记录: |