Discuz教程网

php之CURL函数

[复制链接]
authicon dly 发表于 2011-1-11 19:20:49 | 显示全部楼层 |阅读模式
1、使用CURL模拟POST表单提交
Source.php代码
  1. <?php
  2. //创建curl句柄
  3. $ch = curl_init();
  4. curl_setopt($ch,CURLOPT_URL,"http://localhost/curl/target.php");
  5. curl_setopt($ch,CURLOPT_POST,true);
  6. $data = array('id'=>2,'name'=>'berry');
  7. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //获得返回内容,但不需要直接输出到页面上
  8. curl_setopt($ch,CURLOPT_POSTFIELDS,$data); //POST提交的参数写在一个数组里面
  9. //curl_setopt($ch,CURLOPT_POSTFIELDS,"id=1&name=berry"); //POST提交参数的另一种写法
  10. $output = curl_exec($ch); //执行,并接收返回的内容
  11. curl_close($ch);
  12. echo $output;
  13. ?>
复制代码

Target.php代码
  1. <?php
  2. print_r($_POST);
  3. ?>
复制代码

2、使用CURL判断一个页面是否存在
Php代码
  1. <?php
  2. //创建句柄
  3. $ch = curl_init();
  4. //设置参数
  5. curl_setopt($ch, CURLOPT_URL, “http://www.cnfol.com");
  6. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  7. $output = curl_exec($ch);
  8. //获取页面信息
  9. $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

  10. if ($response_code == ‘404′) {
  11. echo ‘页面不存在‘;
  12. } else {
  13. echo $output;
  14. }
  15. ?>
复制代码




上一篇:MYSQL索引的使用
下一篇:PHP异常处理
authicon shuyangguandan 发表于 2011-2-17 16:12:57 | 显示全部楼层
我也来顶下,一个也不能少!


沭阳游戏网

淮安掼蛋网

牛牛游戏网

扎金花游戏下载
authicon fantuanzi 发表于 2011-6-17 16:00:01 | 显示全部楼层
楼主真强大
authicon kikiya11 发表于 2011-6-17 17:00:21 | 显示全部楼层
好东西要顶的。
authicon forever21 发表于 2011-6-21 00:00:04 | 显示全部楼层
顶啦,不错吧
authicon 卡卡猫 发表于 2011-6-21 22:00:19 | 显示全部楼层
看帖必回
authicon TRACYFLYING 发表于 2011-6-25 14:59:57 | 显示全部楼层
看一下啊,嘻嘻
authicon nmzc 发表于 2011-8-9 15:47:19 | 显示全部楼层
感谢分享  收下了·····
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-10 09:32

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表