php 总结curl 使用教程

来源:互联网 发布:收购淘宝买家资料 编辑:程序博客网 时间:2024/06/16 00:16
默认情况加PHP是不支持CURL的,需要在php.ini中开启该功能
;extension=php_curl.dll前面的分号去掉
  (1)初始化
    curl_init()
  (2)设置变量
    curl_setopt() 。最为重要,一切玄妙均在此。有一长串cURL参数可供设置,它们能指定URL请求的各个细节。要一次性全部看完并理解可能比较困难,所以今天我们只试一下那些更常用也更有用的选项。
  (3)执行并获取结果
    curl_exec()
  (4)释放cURL句柄
    curl_close()

3.1 Get方式实现
复制代码 代码如下:

  //初始化
  $ch = curl_init();
  //设置选项,包括URL
  curl_setopt($ch, CURLOPT_URL, "http://www.jb51.net");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  //执行并获取HTML文档内容
  $output = curl_exec($ch);
  //释放curl句柄
  curl_close($ch);
  //打印获得的数据
  print_r($output);

3.2 Post方式实现
复制代码 代码如下:

   $url = "http://localhost/web_services.php";
  $post_data = array ("username" => "bob","key" => "12345");
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  // post数据
  curl_setopt($ch, CURLOPT_POST, 1);
  // post的变量
  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  $output = curl_exec($ch);
  curl_close($ch);
  //打印获得的数据
  print_r($output);
原创粉丝点击