zoukankan      html  css  js  c++  java
  • PHP中使用cURL实现Get和Post请求的方法

    cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP、FTP、TELNET等。最爽的是,PHP也支持 cURL 库。本文将介绍 cURL 的一些高级特性,以及在PHP中如何运用它。
    cURL实现Get和Post
    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);

    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);

    以上方式获取到的数据是json格式的,使用json_decode函数解释成数组。
    $output_array = json_decode($output,true);
    如果使用json_decode($output)解析的话,将会得到object类型的数据。

  • 相关阅读:
    ios初学者之Tableview的增删移
    ios初学者之在真机上调试应用程序
    androud 自定义属性
    在使用androidStudio中所遇到的错误
    用ticons指令结合ImageMagickDisplay工具批量生成Android适应图片
    android 处理302地址
    Android获取屏幕长宽
    自定义的屏幕适配方法
    android小技巧和注意事项
    redis哨兵&codis
  • 原文地址:https://www.cnblogs.com/daxian2012/p/10142887.html
Copyright © 2011-2022 走看看