PHP常用方法

  • 文件输出(用于输出log):
    file_put_contents(filePath, printLog, FILE_APPEND );

  • 跨域处理 :
    header(‘Access-Control-Allow-Credentials:true’);
    header(‘content-type:application/json;charset=utf8’);

  • 指定允许其他域名访问:
    header(‘Access-Control-Allow-Origin:*’);

  • 响应类型:
    header(‘Access-Control-Allow-Methods:POST’);

  • 响应头设置:
    header(‘Access-Control-Allow-Headers:content-type’);

  • 获取当前时间:
    date(‘y-m-d h:i:s’,time());

  • 数组转换成字符串:
    implode();

  • 字符串转化为整数:
    (int) f o o ; i n t v a l ( foo; intval( foo;intval(foo);

  • 字符串切割成数组,如果是中文,utf-8下应是按3个字节切割,否则为乱码:
    str_split($str,3)

  • 返回字符串长度,如果是中文,utf-8下一个中文返回3个字节长度:
    strlen($str)

  • 返回字符串长度,如果是中文,默认UTF-8编码,返回一个字节长度:
    mb_strlen($str,‘utf-8’)

  • 字符串截取 如果 start 是负数,返回的字符串将从结尾处向前数第 start 个字符开始:
    substr($str,2,2)

  • 截取中文字符串,一个中文按一个字节处理:
    mb_substr($str,2,2,‘utf-8’)

  • 计算字串出现的次数,区分大小写:
    substr_count($str,‘h’)

  • 计算字串出现的次数,区分大小写:
    mb_substr_count($str,‘中’)

  • 首字母大写:
    ucfirst($str)

  • 将字符串中每个单词的首字母转换为大写:
    ucwords($str)

  • 将字符串转化为小写:
    strtolower($str)

  • 将字符串转化为大写:
    strtoupper($str)

  • 字符串翻转,如果含有中文,将会乱码:
    strrev($str)

  • 字串替换,在string中查找第一个参数,替换成第二个参数:
    str_replace (search,replace,$str)

  • 随机打乱一个字符串,中文会乱码:
    str_shuffle ($str)

  • 查找字符串的首次出现 ,第三个参数表示返回是出现前还是出现后,默认false:
    strstr($str,‘wo’,true)

  • 重复一个字符串,返回值为重复这个字符串10次的值:
    str_repeat("-=", 10)

  • 字符串转为时间:
    strtotime (date(“y-m-d h:i:s”)); //当前时间 ,注意H 是24小时 h是12小时

  • PHP发送http请求:

    • 用 file_get_contents 以get方式获取内容:

      $url = 'https://wenda.shukaiming.com/';
      echo file_get_contents($url);
      
    • 用 file_get_contents函数,以post方式获取url

      $data = array(‘foo' => ‘bar');
      $data = http_build_query($data);
      $opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencodedrn' . 'Content-Length: ' . strlen($data) . '\r\n', 'content' => $data));
      $context = stream_context_create($opts);
      $html = file_get_contents('https://wenda.shukaming.com', false, $context);
      echo $html;
      
    • 通过CURL发送get请求

      $ch=curl_init('http://www.xxx.com/xx.html');
      curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
      curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
      $output=curl_exec($ch);
      $fh=fopen("out.html",'w');
      fwrite($fh,$output);
      fclose($fh);
      
    • 通过fsocket发送get请求

      function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30) {
          $socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);
          if (!$socket) die("$errstr($errno)");
          fwrite($socket, "POST $remote_path HTTP/1.0");
          fwrite($socket, "User-Agent: Socket Example");
          fwrite($socket, "HOST: $remote_server");
          fwrite($socket, "Content-type: application/x-www-form-urlencoded");
          fwrite($socket, "Content-length: " . (strlen($post_string) + 8) . "");
          fwrite($socket, "Accept:*/*");
          fwrite($socket, "");
          fwrite($socket, "mypost=$post_string");
          fwrite($socket, "");
          $header = "";
          while ($str = trim(fgets($socket, 4096))) {
              $header .= $str;
          }
          $data = "";
          while (!feof($socket)) {
              $data .= fgets($socket, 4096);
          }
          return $data;
      }
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值