分享几个非常有用的PHP代码片段

2024-06-19 07:08

本文主要是介绍分享几个非常有用的PHP代码片段,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  调用 TextMagic++ API。
  // Include the TextMagicPHP lib
  require('textmagic-sms-api-php/TextMagicAPI.php');
  // Set the username andpassword information
  $username = 'myusername';
  $password = 'mypassword';
  // Create a new instanceof TM
  $router = new TextMagicAPI(array(
  'username' =>$username,
  'password' =>$password
  ));
  // Send a text message to '999-123-4567'
  $result =$router->send('Wake up!', array(9991234567), true);
  // result: Result is: Array( [messages] => Array ( [19896128] => 9991234567)[sent_text] => Wake up! [parts_count] => 1 )
  2. 根据IP查找地址
  function detect_city($ip){
  $default = 'UNKNOWN';
  if (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost')
  $ip = '8.8.8.8';
  $curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)';
  $url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip);
  $ch = curl_init();
  $curl_opt = array(
  CURLOPT_FOLLOWLOCATION => 1,
  CURLOPT_HEADER => 0,
  CURLOPT_RETURNTRANSFER=> 1,
  CURLOPT_USERAGENT=> $curlopt_useragent,
  CURLOPT_URL => $url,
  CURLOPT_TIMEOUT => 1,
  CURLOPT_REFERER => 'http://' .$_SERVER['HTTP_HOST'],
  );
  curl_setopt_array($ch,$curl_opt);
  $content = curl_exec($ch);
  if (!is_null($curl_info)) {
  $curl_info = curl_getinfo($ch);
  }
  curl_close($ch);
  if ( preg_match('{
  City : ([^<]*)
  }i', $content, $regs) ) {
  $city = $regs[1];
  }
  if ( preg_match('{
  State/Province : ([^<]*)
  }i', $content, $regs) ) {
  $state = $regs[1];
  }
  if( $city!='' && $state!=''){
  $location = $city . ', ' .$state;
  return $location;
  }else{
  return $default;
  }
  }
  3. 显示网页的源代码
  $lines = file('http://google.com/');
  foreach ($lines as$line_num => $line) {
  // loop thru each line and prepend line numbers
  echo "Line # {$line_num} : " . htmlspecialchars($line) . "
\n";
  }
  4. 检查服务器是否使用HTTPS
  if ($_SERVER['HTTPS']!= "on") {
  echo "This is not HTTPS";
  }else{
  echo "This is HTTPS";
  }
  5. 显示Facebook粉丝数量
  function fb_fan_count($facebook_name){
  // Example: https://graph.facebook.com/digimantra
  $data = json_decode(file_get_contents("https://graph.facebook.com/".$facebook_name));
  echo $data->likes;
  }
  6. 检测图片的主要颜色
  $i = imagecreatefromjpeg("image.jpg");
  for ($x=0;$x
  for ($y=0;$y
  $rgb = imagecolorat($i,$x,$y);
  $r = ($rgb >> 16) & 0xFF;
  $g = ($rgb >> & 0xFF;
  $b = $rgb & 0xFF;
  $rTotal += $r;
  $gTotal += $g;
  $bTotal += $b;
  $total++;
  }
  }
  $rAverage = round($rTotal/$total);
  $gAverage = round($gTotal/$total);
  $bAverage = round($bTotal/$total);
  7. 获取内存使用信息
  echo "Initial:".memory_get_usage()." bytes \n";
  /* prints
  Initial: 361400 bytes
  */
  // let's use up some memory
  for ($i = 0; $i < 100000;$i++) {
  $array []= md5($i);
  }
  // let's remove half of the array
  for ($i = 0; $i < 100000;$i++) {
  unset($array[$i]);
  }
  echo "Final:".memory_get_usage()." bytes \n";
  /* prints
  Final: 885912 bytes
  */
  echo "Peak:".memory_get_peak_usage()." bytes \n";
  /* prints
  Peak: 13687072 bytes
  */
  8. 使用 gzcompress() 压缩数据
  $string =
  "Lorem ipsum dolor sit amet, consectetur
  adipiscing elit. Nunc ut elit id mi ultricies
  adipiscing. Nulla facilisi. Praesent pulvinar,
  sapien vel feugiat vestibulum, nulla dui pretiumorci,
  non ultricies elit lacus quis ante. Lorem ipsum dolor
  sit amet, consectetur adipiscing elit. Aliquam
  pretium ullamcorper urna quis iaculis. Etiam ac massa
  sed turpis tempor luctus.Curabitur sed nibh eu elit
  mollis congue. Praesent ipsum diam, consectetur vitae
  ornare a, aliquam a nunc. In id magna pellentesque
  tellus posuere adipiscing. Sed non mi metus, at lacinia
  augue. Sed magna nisi, ornare in mollis in, mollis
  sed nunc. Etiam at justoin leo congue mollis.
  Nullam in neque eget metus hendrerit scelerisque
  eu non enim. Ut malesuada lacus eu nulla bibendum
  id euismod urna sodales.";
  $compressed = gzcompress($string);
  echo "Original size: ". strlen($string)."\n";
  /* prints
  Original size: 800
  */
  echo "Compressed size:". strlen($compressed)."\n";
  /* prints
  Compressed size: 418
  */
  // getting it back
  $original = gzuncompress($compressed);
  9. 使用PHP做Whois检查
  function whois_query($domain) {
  // fix the domain name:
  $domain = strtolower(trim($domain));
  $domain = preg_replace('/^http:\/\//i', '', $domain);
  $domain = preg_replace('/^www\./i', '',$domain);
  $domain = explode('/',$domain);
  $domain = trim($domain[0]);
  // split the TLD from domain name
  $_domain = explode('.',$domain);
  $lst = count($_domain)-1;
  $ext = $_domain[$lst];
  // You find resources and lists
  // like these on wikipedia:
  //
  // http://de.wikipedia.org/wiki/Whois
  //
  $servers = array(
  "biz" =>"whois.neulevel.biz",
  "com" =>"whois.internic.net",
  "us" => "whois.nic.us",
  "coop" =>"whois.nic.coop",
  "info" =>"whois.nic.info",
  "name" =>"whois.nic.name",
  "net" =>"whois.internic.net",
  "gov" =>"whois.nic.gov",
  "edu" =>"whois.internic.net",
  "mil" =>"rs.internic.net",
  "int" =>"whois.iana.org",
  "ac" => "whois.nic.ac",
  "ae" =>"whois.uaenic.ae",
  "at" => "whois.ripe.net",
  "au" =>"whois.aunic.net",
  "be" => "whois.dns.be",
  "bg" =>"whois.ripe.net",
  "br" =>"whois.registro.br",
  "bz" =>"whois.belizenic.bz",
  "ca" => "whois.cira.ca",
  "cc" => "whois.nic.cc",
  "ch" => "whois.nic.ch",
  "cl" => "whois.nic.cl",
  "cn" =>"whois.cnnic.net.cn",
  "cz" => "whois.nic.cz",
  "de" => "whois.nic.de",
  "fr" => "whois.nic.fr",
  "hu" => "whois.nic.hu",
  "ie" =>"whois.domainregistry.ie",
  "il" =>"whois.isoc.org.il",
  "in" =>"whois.ncst.ernet.in",
  "ir" => "whois.nic.ir",
  "mc" =>"whois.ripe.net",
  "to" =>"whois.tonic.to",
  "tv" => "whois.tv",
  "ru" =>"whois.ripn.net",
  "org" => "whois.pir.org",
  "aero" =>"whois.information.aero",
  "nl" => "whois.domain-registry.nl"
  );
  if (!isset($servers[$ext])){
  die('Error: No matching nic server found!');
  }
  $nic_server =$servers[$ext];
  $output = '';
  // connect to whois server:
  if ($conn = fsockopen ($nic_server, 43)) {
  fputs($conn,$domain."\r\n");
  while(!feof($conn)) {
  $output .= fgets($conn,128);
  }
  fclose($conn);
  }
  else { die('Error: Could not connect to ' . $nic_server. '!'); }
  return $output;
  }
  10. 通过Email发送PHP错误
  // Our custom error handler
  function nettuts_error_handler($number, $message, $file, $line,$vars){
  $email = "
An error ($number) occurred on line
$line and in the file: $file.
$message
";
  $email .= "
" . print_r($vars, 1) . "
";
  $headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  // Email the error to someone...
  error_log($email, 1, 'you@youremail.com',$headers);
  // Make sure that you decide how to respond to errors (on the user's side)
  // Either echo an error message, or kill the entire project. Up to you...
  // The code below ensures that we only "die" if the error was more than
  // just a NOTICE.
  if ( ($number !== E_NOTICE) && ($number < 2048) ) {
  die("There was an error.Please try again later.");
  }
  }
  // We should use our custom function to handle errors.
  set_error_handler('nettuts_error_handler');
  // Trigger an error... (vardoesn't exist)
  echo$somevarthatdoesnotexist;

原文地址:http://bbs.lampbrother.net/read-htm-tid-119002.html

<script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/buttonLite.js#style=-1&uuid=&pophcol=3&lang=zh"></script> <script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/bshareC0.js"></script>
阅读(80) | 评论(0) | 转发(0) |
0

上一篇:说说第二项目

下一篇:PHP面向对象法则

相关热门文章
  • C++ 将unsigned char数组 ...
  • linux内核的一些预定义...
  • 美国RT服务器租用仿牌网站不二...
  • 北京外资公司注册都需要那些流...
  • text段,data段,bss段,堆和栈 ...
  • IP Sec VPN与NAT破镜重圆
  • 网站导航
  • GoAgent图文设置教程
  • UT2.0正式版下载
  • tomcat6.0配置(含配置视频下载...
  • 大家都是用什么来管理hadoop集...
  • 网站被人挂了吗,添加了些程序...
  • Nginx如何保证不走宕机的那个...
  • 大家谈谈MYSQL客户端和服务器...
  • 以下代码运行后为何会输出5?...
给主人留下些什么吧!~~
评论热议

这篇关于分享几个非常有用的PHP代码片段的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1074340

相关文章

jupyter代码块没有运行图标的解决方案

《jupyter代码块没有运行图标的解决方案》:本文主要介绍jupyter代码块没有运行图标的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录jupyter代码块没有运行图标的解决1.找到Jupyter notebook的系统配置文件2.这时候一般会搜索到

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

用js控制视频播放进度基本示例代码

《用js控制视频播放进度基本示例代码》写前端的时候,很多的时候是需要支持要网页视频播放的功能,下面这篇文章主要给大家介绍了关于用js控制视频播放进度的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言html部分:JavaScript部分:注意:总结前言在javascript中控制视频播放

Spring Boot 3.4.3 基于 Spring WebFlux 实现 SSE 功能(代码示例)

《SpringBoot3.4.3基于SpringWebFlux实现SSE功能(代码示例)》SpringBoot3.4.3结合SpringWebFlux实现SSE功能,为实时数据推送提供... 目录1. SSE 简介1.1 什么是 SSE?1.2 SSE 的优点1.3 适用场景2. Spring WebFlu

java之Objects.nonNull用法代码解读

《java之Objects.nonNull用法代码解读》:本文主要介绍java之Objects.nonNull用法代码,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录Java之Objects.nonwww.chinasem.cnNull用法代码Objects.nonN

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

python+opencv处理颜色之将目标颜色转换实例代码

《python+opencv处理颜色之将目标颜色转换实例代码》OpenCV是一个的跨平台计算机视觉库,可以运行在Linux、Windows和MacOS操作系统上,:本文主要介绍python+ope... 目录下面是代码+ 效果 + 解释转HSV: 关于颜色总是要转HSV的掩膜再标注总结 目标:将红色的部分滤