分享几个非常有用的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

相关文章

[职场] 护理专业简历怎么写 #经验分享#微信

护理专业简历怎么写   很多想成为一名护理方面的从业者,但是又不知道应该怎么制作一份简历,现在这里分享了一份护理方面的简历模板供大家参考。   蓝山山   年龄:24   号码:12345678910   地址:上海市 邮箱:jianli@jianli.com   教育背景   时间:2011-09到2015-06   学校:蓝山大学   专业:护理学   学历:本科

大学湖北中医药大学法医学试题及答案,分享几个实用搜题和学习工具 #微信#学习方法#职场发展

今天分享拥有拍照搜题、文字搜题、语音搜题、多重搜题等搜题模式,可以快速查找问题解析,加深对题目答案的理解。 1.快练题 这是一个网站 找题的网站海量题库,在线搜题,快速刷题~为您提供百万优质题库,直接搜索题库名称,支持多种刷题模式:顺序练习、语音听题、本地搜题、顺序阅读、模拟考试、组卷考试、赶快下载吧! 2.彩虹搜题 这是个老公众号了 支持手写输入,截图搜题,详细步骤,解题必备

uniapp接入微信小程序原生代码配置方案(优化版)

uniapp项目需要把微信小程序原生语法的功能代码嵌套过来,无需把原生代码转换为uniapp,可以配置拷贝的方式集成过来 1、拷贝代码包到src目录 2、vue.config.js中配置原生代码包直接拷贝到编译目录中 3、pages.json中配置分包目录,原生入口组件的路径 4、manifest.json中配置分包,使用原生组件 5、需要把原生代码包里的页面修改成组件的方

公共筛选组件(二次封装antd)支持代码提示

如果项目是基于antd组件库为基础搭建,可使用此公共筛选组件 使用到的库 npm i antdnpm i lodash-esnpm i @types/lodash-es -D /components/CommonSearch index.tsx import React from 'react';import { Button, Card, Form } from 'antd'

17.用300行代码手写初体验Spring V1.0版本

1.1.课程目标 1、了解看源码最有效的方式,先猜测后验证,不要一开始就去调试代码。 2、浓缩就是精华,用 300行最简洁的代码 提炼Spring的基本设计思想。 3、掌握Spring框架的基本脉络。 1.2.内容定位 1、 具有1年以上的SpringMVC使用经验。 2、 希望深入了解Spring源码的人群,对 Spring有一个整体的宏观感受。 3、 全程手写实现SpringM

[职场] 公务员的利弊分析 #知识分享#经验分享#其他

公务员的利弊分析     公务员作为一种稳定的职业选择,一直备受人们的关注。然而,就像任何其他职业一样,公务员职位也有其利与弊。本文将对公务员的利弊进行分析,帮助读者更好地了解这一职业的特点。 利: 1. 稳定的职业:公务员职位通常具有较高的稳定性,一旦进入公务员队伍,往往可以享受到稳定的工作环境和薪资待遇。这对于那些追求稳定的人来说,是一个很大的优势。 2. 薪资福利优厚:公务员的薪资和

代码随想录算法训练营:12/60

非科班学习算法day12 | LeetCode150:逆波兰表达式 ,Leetcode239: 滑动窗口最大值  目录 介绍 一、基础概念补充: 1.c++字符串转为数字 1. std::stoi, std::stol, std::stoll, std::stoul, std::stoull(最常用) 2. std::stringstream 3. std::atoi, std

android一键分享功能部分实现

为什么叫做部分实现呢,其实是我只实现一部分的分享。如新浪微博,那还有没去实现的是微信分享。还有一部分奇怪的问题:我QQ分享跟QQ空间的分享功能,我都没配置key那些都是原本集成就有的key也可以实现分享,谁清楚的麻烦详解下。 实现分享功能我们可以去www.mob.com这个网站集成。免费的,而且还有短信验证功能。等这分享研究完后就研究下短信验证功能。 开始实现步骤(新浪分享,以下是本人自己实现

记录AS混淆代码模板

开启混淆得先在build.gradle文件中把 minifyEnabled false改成true,以及shrinkResources true//去除无用的resource文件 这些是写在proguard-rules.pro文件内的 指定代码的压缩级别 -optimizationpasses 5 包明不混合大小写 -dontusemixedcaseclassnames 不去忽略非公共

麻了!一觉醒来,代码全挂了。。

作为⼀名程序员,相信大家平时都有代码托管的需求。 相信有不少同学或者团队都习惯把自己的代码托管到GitHub平台上。 但是GitHub大家知道,经常在访问速度这方面并不是很快,有时候因为网络问题甚至根本连网站都打不开了,所以导致使用体验并不友好。 经常一觉醒来,居然发现我竟然看不到我自己上传的代码了。。 那在国内,除了GitHub,另外还有一个比较常用的Gitee平台也可以用于