`
yushine
  • 浏览: 201391 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

isolated 十个超级有用的PHP代码片段

    博客分类:
  • PHP
 
阅读更多

1. 发送短信
调用 TextMagic API。

// Include the TextMagic PHP lib
require('textmagic-sms-api-php/TextMagicAPI.php');

// Set the username and password information
$username = 'myusername';
$password = 'mypassword';

// Create a new instance of 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('{<li>City : ([^<]*)</li>}i', $content, $regs) ) {
$city = $regs[1];
}
if ( preg_match('{<li>State/Province : ([^<]*)</li>}i', $content, $regs) ) {
$state = $regs[1];
}

if( $city!='' && $state!='' ){
$location = $city . ', ' . $state;
return$location;
}else{
return$default;
}

}


3. 显示网页的源代码

<?php // display source code
$lines = file('http://google.com/');
foreach ($lines as $line_num => $line) {
// loop thru each line and prepend line numbers
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br>\n";
}


4. 检查服务器是否使用HTTPS

if ($_SERVER['HTTPS'] != "on") {
echo "This is not HTTPS";
}else{
echo "This is HTTPS";
}


5. 显示Faceboo**丝数量

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<imagesx($i);$x++) {
for ($y=0;$y<imagesy($i);$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
*/
// http://www.baoluowanxiang.com/
// 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 pretium orci,
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 justo in 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错误

<?php

// Our custom error handler
function nettuts_error_handler($number, $message, $file, $line, $vars){
$email = "
<p>An error ($number) occurred on line
<strong>$line</strong> and in the <strong>file: $file.</strong>
<p> $message </p>";

$email .= "<pre>" . print_r($vars, 1) . "</pre>";

$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... (var doesn't exist)
echo$somevarthatdoesnotexist;

分享到:
评论

相关推荐

    Visual Studio 2013 Shell (Isolated)

    7. **智能代码编辑**:即使是 Isolated Shell,也支持智能代码编辑功能,如语法高亮、自动完成、代码重构等,这些特性对于提高开发效率有着不可替代的作用。 8. **多语言支持**:通过安装相应的组件和服务,...

    isolated-vm

    "isolated-vm"是一个与虚拟化技术相关的概念,它通常指的是在计算机系统中创建一个独立、隔离的虚拟环境,这种环境称为隔离的虚拟机。在这个环境中,操作系统和其他应用程序可以运行,而不会影响主机系统或其他...

    WP7 Isolated Storage Explorer

    WP7 Isolated Storage Explorer是一款专为Windows Phone 7(WP7)平台设计的工具,用于帮助开发者和用户方便地查看、管理和操作手机上的Isolated Storage。Isolated Storage是.NET框架提供的一种安全、隔离的存储...

    Isolated vRouter Setup and Testing.docx

    【Isolated vRouter Setup and Testing】文档主要涵盖了在隔离环境中设置和测试虚拟路由器(vRouter)的详细步骤,以及使用Trex测试方法进行性能评估。本文档由Intel公司的Joseph Gasparakis、Subarna Kar、Savannah ...

    window phone 8 Backup Isolated Storage To SkyDrive

    Isolated Storage类似于每个应用自己的私有文件系统,确保了不同应用间的数据隔离,同时保护用户数据不被其他应用访问。 备份Isolated Storage至SkyDrive是WP8用户常用的一种数据保护手段。SkyDrive(现称为One...

    40.5 W Non-Isolated Buck LED Driver

    标题“40.5 W Non-Isolated Buck LED Driver”和描述“led驱动40.5 W, High Power Factor (-0.95), -5% IOUT Tolerance, Non-Isolated Buck LED Driver Using LinkSwitchTM-PH LNK419EG”共同指出了这款LED驱动器的...

    IsolatedStorageDemo

    IsolatedStorageDemo是一个用于演示.NET框架中Isolated Storage功能的源代码示例。Isolated Storage是.NET Framework提供的一种安全且受控的存储区域,它允许应用程序在用户的机器上存储数据,同时保持与其他应用...

    power_SCIG0.rar_INDUCTION GENERATOR_Isolated grid_in_isolated

    标题中的"power_SCIG0.rar_INDUCTION GENERATOR_Isolated grid_in_isolated"表明这是一个关于交流感应发电机(Induction Generator,简称IG)在离网(Isolated Grid)环境下运行的资源包。描述简单明了,"induction ...

    visual_studio_2010_shell_isolated_x86

    1、sqlserver 2012 因为卸载vs2010后无法正常使用,需要重新安装该插件

    isolated-vm:适用于Node.js的安全且隔离的JS环境

    isolated-vm-访问Node.js中的多个隔离 isolated-vm是nodejs的库,可让您访问v8的Isolate接口。 这使您可以创建彼此完全隔离的JavaScript环境。 如果您需要以安全的方式运行一些不受信任的代码,则可能会发现此模块...

    Troubleshooting C C++ Isolated Applications and Side-by-side Assemblies

    Troubleshooting C C++ Isolated Applications and Side-by-side Assemblies. 发现并解决C C++独立应用程序和并行程序集的问题(无法运行VS2008开发的MFCC++程序的参考)

    Windows+Phone+7+完美开发征程源代码

    综上所述,"Windows Phone 7 完美开发征程源代码"是一个深入学习WP7开发的宝库,无论你是刚接触这个平台的新手,还是希望提升现有技能的开发者,都能从中受益。通过实际的代码示例,你可以更好地理解和运用WP7的开发...

    simple-isolated-word-recognition.rar_ Isolated_isolated_word rec

    可以实现简单孤立词的识别和训练,内有1到9的录音,可以自己训练。

    Low Noise Isolated Power Supply【5V转±15V,隔离输出,超低纹波】

    Low Noise Isolated Power Supply的设计主要基于以下几个方面: 1. 隔离输出: Low Noise Isolated Power Supply能够提供±15V的隔离输出,这使得其能够满足各种应用场景的需求。 2. 超低纹波:该电源解决方案具有...

    艾络格电子工业 RS232 & RS485/422 Isolated 转换器.zip

    在当今高度发达的工业自动化领域,通信技术的发展直接关系到整个生产系统的稳定性和可靠性。随着各种工业设备的广泛应用,不同类型设备间的通信兼容性问题也随之凸显。为了应对这一挑战,艾络格电子工业推出了RS232 ...

    windows phone8代码

    每个章节的源代码实例都是为了帮助开发者理解并掌握特定的技术,通过实际操作来巩固理论知识。这些实例可以帮助开发者逐步提升技能,最终能够独立完成Windows Phone 8应用程序的开发。通过深入研究和实践这些代码,...

    《Windows Phone 7高级编程》随书源代码

    源代码可能包含如何使用Bing Maps API进行地图操作和获取用户位置的代码片段。 4. **多媒体与相机支持**:WP7支持多媒体文件播放和拍照功能,开发者可以通过多媒体API实现视频录制、照片拍摄和音频播放。书中可能会...

    windows phone 官方示例代码

    这个官方示例代码集合针对Windows Phone 8.1版本,提供了全面的开发指导和实践案例,帮助开发者深入理解和应用各种功能。 1. **控件使用**: Windows Phone 8.1的UI设计基于XAML(Extensible Application Markup ...

    chrome-extension-isolated-cookie:每个标签都有单独的 cookie

    在Chrome浏览器中,扩展程序(Extension)的Cookie管理机制与普通网页有所不同,这主要涉及到一个被称为"isolated cookie"的概念。这个特性确保了每个标签页内的扩展程序具有独立的Cookie存储,防止不同标签页间的...

Global site tag (gtag.js) - Google Analytics