今天工作较清闲,便看了下curl相关资料,晚上时便整理了下,主要是代码为准
curl的get请求
$data = 'this data is from client';
$ch = curl_init('http://www.test.com/server.php?info='. urlencode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
curl的post请求
$data = array(
'username' => 'username',
'passwd' => 'passwd'
);
$ch = curl_init('http://www.test.com/server.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
或是
/*
$data = array(
'username' => 'username',
'passwd' => 'passwd'
);
这种形式的就不用implode了
*/
$data = array(
'username=shuxue',
'password=shuxue',
);
$ch = curl_init('http://www.test.com/server.php');
// 将curl_exec()获取的信息以文件流的形式返回,而不是直接输出
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。
curl_setopt($ch, CURLOPT_POST,1);
// 全部数据使用HTTP协议中的"POST"操作来发送
curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $data));
$output = curl_exec($ch);
curl_close($ch);
echo $output;
curl cookie
$data = array(
'username' => 'username',
'password' => 'password'
);
$cookie = tempnam('./', 'cookie');
$ch = curl_init('http://112.124.24.77/Public/authLogin');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_exec($ch);
curl_close($ch);
$ch2 = curl_init('http://112.124.24.77/Index/my');
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie);
$output = curl_exec($ch2);
curl_close($ch2);
echo $output;
curl上传文件
header('content-type:text/html;charset=utf8');
$ch = curl_init('http://test/1606/file.php');
$post_data = array(
// 'img' => '@2.xls', // 语法在5.5就已经被打了deprecated,在5.6中就直接被删除了
// 'img' => new CURLFile('2.xls'), // PHP从5.5开始引入了新的CURLFile类用来指向文件。CURLFile类也可以详细定义MIME类型、文件名等可能出现在multipart/form-data数据中的附加信息
'img' => new CURLFile('2.xls','application/vnd.ms-excel','123.xls'),
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
echo json_decode($output);
if($_FILES){
$filename = $_FILES['img']['name'];
$tmpname = $_FILES['img']['tmp_name'];
if(move_uploaded_file($tmpname,dirname(__FILE__).'/upload/'.$filename)){
echo json_encode('上传成功');
}else{
$data = json_encode($_FILES);
echo $data;
}
} else {
echo json_encode(['status' => 0, 'msg' => 'no file']);
}
curl验证链接是否失效
header('Content-Type:text/html;Charset=utf8');
$url = array('http://www.baidu.com/');
$active = null;
$mh = curl_multi_init();
foreach ($url as $value) {
add_url_to_multi($value, $mh);
}
function add_url_to_multi($url, $mh) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_multi_add_handle($mh, $ch);
}
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
if ($mhinfo = curl_multi_info_read($mh)) {
$chinfo = curl_getinfo($mhinfo['handle']);
print_r($chinfo);
if (!$chinfo['http_code']) {
echo '死链接';
}
elseif ($chinfo['http_code'] == 404) {
echo '404';
}
else {
echo '正常' . $chinfo['url'];
}
curl_multi_remove_handle($mh, $mhinfo['handle']);
curl_close($mhinfo['handle']);
}
}
}
curl_multi_close($mh);
详细解释的
// 初始化一些变量
$max_connections = 2;
$url_list = array('http://www.baidu.com/', 'http://www.abcd.com/');
$working_urls = array();
$dead_urls = array();
$not_found_urls = array();
$active = null;
// 1. 批处理器
// 新建一个批处理器。Created a multi handle.
$mh = curl_multi_init();
// 2. 加入需批量处理的URL
// 稍后我们将创建一个把URL加入批处理器的函数 add_url_to_multi_handle() 。
// 每当这个函数被调用,就有一个新url被加入批处理器。
// 一开始,我们给批处理器添加了10个URL(这一数字由 $max_connections 所决定)
for ($i = 0; $i < $max_connections; $i++) {
add_url_to_multi_handle($mh, $url_list);
}
// 3. 初始处理
// 运行 curl_multi_exec() 进行初始化工作是必须的,只要它返回 CURLM_CALL_MULTI_PERFORM 就还有事情要做。
// 这么做主要是为了创建连接,它不会等待完整的URL响应。
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
// 4. 主循环
// 只要批处理中还有活动连接主循环就会一直持续。
while ($active && $mrc == CURLM_OK) {
// 5. 有活动连接
// curl_multi_select() 会一直等待,直到某个URL查询产生活动连接。
if (curl_multi_select($mh) != -1) {
// 6. 干活
// cURL的活儿又来了,主要是获取响应数据。
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
// 7. 有信息否?
// 检查各种信息。当一个URL请求完成时,会返回一个数组。
if ($mhinfo = curl_multi_info_read($mh)) {
// 意味着该连接正常结束
// 8. 从curl句柄获取信息
// 在返回的数组中有一个 cURL 句柄。我们利用其获取单个cURL请求的相应信息。
$chinfo = curl_getinfo($mhinfo['handle']);
// 9. 死链么?
// 如果这是一个死链或者请求超时,不会返回http状态码。
if (!$chinfo['http_code']) {
$dead_urls []= $chinfo['url'];
// 10. 404了?
// 如果这个页面找不到了,会返回404状态码。
} else if ($chinfo['http_code'] == 404) {
$not_found_urls []= $chinfo['url'];
// 11. 还能用
// 其他情况我们都认为这个链接是可用的(当然,你也可以再检查一下500错误之类...)。
} else {
$working_urls []= $chinfo['url'];
}
// 12. 移除句柄
// 从该批次移除这个cURL句柄,因为它已经没有利用价值了,关了它!
curl_multi_remove_handle($mh, $mhinfo['handle']);
curl_close($mhinfo['handle']);
}
}
}
// 13. 完了
// 嗯,该干的都干了。关闭批处理器,生成报告。
curl_multi_close($mh);
echo "==Dead URLs==\n";
echo implode("\n",$dead_urls) . "\n\n";
echo "==404 URLs==\n";
echo implode("\n",$not_found_urls) . "\n\n";
echo "==Working URLs==\n";
echo implode("\n",$working_urls);
// 14. 向批处理器添加url
// 回过头来看给批处理器添加新URL的函数。
// 这个函数每调用一次,静态变量 $index 就递增一次,这样我们才能知道还剩多少URL没处理。
function add_url_to_multi_handle($mh, $url_list) {
static $index = 0;
global $max_connections;
// 如果还剩url没用
if (($index <= $max_connections - 1) && $url_list[$index]) {
// 新建curl句柄
$ch = curl_init();
// 配置url
curl_setopt($ch, CURLOPT_URL, $url_list[$index]);
// 不想输出返回的内容
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 重定向到哪儿我们就去哪儿
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// 不需要内容体,能够节约带宽和时间
curl_setopt($ch, CURLOPT_NOBODY, 1);
// 加入到批处理器中
curl_multi_add_handle($mh, $ch);
// 拨一下计数器,下次调用该函数就能添加下一个url了
$index++;
return true;
} else {
// 没有新的URL需要处理了
return false;
}
}
curl资料 http://www.chinaz.com/program/2010/0119/104346.shtml