加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

PHP实用代码片段(一)

发布时间:2020-05-25 03:07:03 所属栏目:PHP 来源:互联网
导读:1. 发送 SMS在开发 Web 或者移动应用的时候,经常会遇到需要发送 SMS 给用户,或者因为登录原因,或者是为了发送信息。下面的 PHP 代码就实现了发送 SMS 的功能。为了使用任何的语言发送 SMS,需要一个 SMS gateway。大部分的 SMS 会提供一个 API,这里是使用

1. 发送 SMS

在开发 Web 或者移动应用的时候,经常会遇到需要发送 SMS 给用户,或者因为登录原因,或者是为了发送信息。下面的 PHP 代码就实现了发送 SMS 的功能。

为了使用任何的语言发送 SMS,需要一个 SMS gateway。大部分的 SMS 会提供一个 API,这里是使用 MSG91 作为 SMS gateway。

send_sms(, = "XXXXXXXXXXX""Asia/Kolkata" = ("%Y-%m-%d %H:%M:%S" = <span style="color: #008000">//<span style="color: #008000">Sender ID,While using route4 sender id should be 6 characters long.
<span style="color: #800080">$senderId = "IKOONK"<span style="color: #000000">;

<span style="color: #008000">//<span style="color: #008000">Your message to send,Add URL encoding here.
<span style="color: #800080">$message = <span style="color: #008080">urlencode(<span style="color: #800080">$msg<span style="color: #000000">);

<span style="color: #008000">//<span style="color: #008000">Define route
<span style="color: #800080">$route = "template"<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">Prepare you post parameters
<span style="color: #800080">$postData = <span style="color: #0000ff">array<span style="color: #000000">(
'authkey' => <span style="color: #800080">$authKey,'mobiles' => <span style="color: #800080">$mobileNumber,'message' => <span style="color: #800080">$message,'sender' => <span style="color: #800080">$senderId,'route' => <span style="color: #800080">$route<span style="color: #000000">
);

<span style="color: #008000">//<span style="color: #008000">API URL
<span style="color: #800080">$url="https://control.msg91.com/sendhttp.php"<span style="color: #000000">;

<span style="color: #008000">//<span style="color: #008000"> init the resource
<span style="color: #800080">$ch =<span style="color: #000000"> curl_init();
curl_setopt_array(<span style="color: #800080">$ch,<span style="color: #0000ff">array<span style="color: #000000">(
CURLOPT_URL => <span style="color: #800080">$url,<span style="color: #000000">
CURLOPT_RETURNTRANSFER => <span style="color: #0000ff">true,<span style="color: #000000">
CURLOPT_POST => <span style="color: #0000ff">true,<span style="color: #000000">
CURLOPT_POSTFIELDS => <span style="color: #800080">$postData
<span style="color: #008000">//<span style="color: #008000">,CURLOPT_FOLLOWLOCATION => true
<span style="color: #000000">));

<span style="color: #008000">//<span style="color: #008000">Ignore SSL certificate verification
curl_setopt(<span style="color: #800080">$ch,CURLOPT_SSL_VERIFYHOST,0<span style="color: #000000">);
curl_setopt(<span style="color: #800080">$ch,CURLOPT_SSL_VERIFYPEER,0<span style="color: #000000">);

<span style="color: #008000">//<span style="color: #008000">get response
<span style="color: #800080">$output = curl_exec(<span style="color: #800080">$ch<span style="color: #000000">);
<span style="color: #008000">//<span style="color: #008000">Print error if any
<span style="color: #0000ff">if(curl_errno(<span style="color: #800080">$ch<span style="color: #000000">))
{
<span style="color: #0000ff">echo 'error:' . curl_error(<span style="color: #800080">$ch<span style="color: #000000">);
}

curl_close(<span style="color: #800080">$ch<span style="color: #000000">);
}

其中“$authKey = "XXXXXXXXXXX";”需要你输入你的密码,“$senderId = "IKOONK";”需要你输入你的 SenderID。当输入移动号码的时候需要指定国家代码 (比如,美国是 1,印度是 91 )。

语法:

2. 使用 mandrill 发送邮件

Mandril 是一款强大的 SMTP 提供器。开发者倾向于使用一个第三方 SMTP provider 来获取更好的收件交付。

下面的函数中,你需要把 “Mandrill.php” 放在同一个文件夹,作为 PHP 文件,这样就可以使用TA来发送邮件。

send_email(,, 'Mandrill.php' = 'XXXXXXXXXX'; = Mandrill(<span style="color: #800080">$message = <span style="color: #0000ff">new<span style="color: #000000"> stdClass();
<span style="color: #800080">$message->html = <span style="color: #800080">$message1<span style="color: #000000">;
<span style="color: #800080">$message->text = <span style="color: #800080">$message1<span style="color: #000000">;
<span style="color: #800080">$message->subject = <span style="color: #800080">$subject<span style="color: #000000">;
<span style="color: #800080">$message->from_email = "blog@koonk.com";<span style="color: #008000">//<span style="color: #008000">Sender Email
<span style="color: #800080">$message->from_name = "KOONK";<span style="color: #008000">//<span style="color: #008000">Sender Name
<span style="color: #800080">$message->to = <span style="color: #0000ff">array(<span style="color: #0000ff">array("email" => <span style="color: #800080">$to_email<span style="color: #000000">));
<span style="color: #800080">$message->track_opens = <span style="color: #0000ff">true<span style="color: #000000">;

<span style="color: #800080">$response = <span style="color: #800080">$mandrill->messages->send(<span style="color: #800080">$message<span style="color: #000000">);
}

“$apikey = 'XXXXXXXXXX'; //specify your api key here”这里需要你指定你的 API 密钥(从 Mandrill 账户中获得)。

语法:

为了达到最好的效果,最好按照 Mandrill 的教程去配置 DNS。

3. PHP 函数:阻止 SQL 注入

SQL 注入或者 SQLi 常见的攻击网站的手段,使用下面的代码可以帮助你防止这些注入。

clean( (( ( => [] = clean(clean($val); = () ( = ( = (,ENT_QUOTES,'UTF-8'

语法:

4. 检测用户位置

使用下面的函数,可以检测用户是在哪个城市访问你的网站。

detect_city( </span><span style="color: #800080"&gt;$default</span> = 'UNKNOWN'<span style="color: #000000"&gt;; </span><span style="color: #800080"&gt;$curlopt_useragent</span> = '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)'<span style="color: #000000"&gt;; </span><span style="color: #800080"&gt;$url</span> = 'http://ipinfodb.com/ip_locator.php?ip=' . <span style="color: #008080"&gt;urlencode</span>(<span style="color: #800080"&gt;$ip</span><span style="color: #000000"&gt;); </span><span style="color: #800080"&gt;$ch</span> =<span style="color: #000000"&gt; curl_init(); </span><span style="color: #800080"&gt;$curl_opt</span> = <span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;( CURLOPT_FOLLOWLOCATION </span>=> 1,<span style="color: #000000"&gt; CURLOPT_HEADER </span>=> 0,<span style="color: #000000"&gt; CURLOPT_RETURNTRANSFER </span>=> 1,<span style="color: #000000"&gt; CURLOPT_USERAGENT </span>=> <span style="color: #800080"&gt;$curlopt_useragent</span>,<span style="color: #000000"&gt; CURLOPT_URL </span>=> <span style="color: #800080"&gt;$url</span>,<span style="color: #000000"&gt; CURLOPT_TIMEOUT </span>=> 1,<span style="color: #000000"&gt; CURLOPT_REFERER </span>=> 'http://' . <span style="color: #800080"&gt;$_SERVER</span>['HTTP_HOST'],<span style="color: #000000"&gt; ); curl_setopt_array(</span><span style="color: #800080"&gt;$ch</span>,<span style="color: #800080"&gt;$curl_opt</span><span style="color: #000000"&gt;); </span><span style="color: #800080"&gt;$content</span> = curl_exec(<span style="color: #800080"&gt;$ch</span><span style="color: #000000"&gt;); </span><span style="color: #0000ff"&gt;if</span> (!<span style="color: #008080"&gt;is_null</span>(<span style="color: #800080"&gt;$curl_info</span><span style="color: #000000"&gt;)) { </span><span style="color: #800080"&gt;$curl_info</span> = curl_getinfo(<span style="color: #800080"&gt;$ch</span><span style="color: #000000"&gt;); } curl_close(</span><span style="color: #800080"&gt;$ch</span><span style="color: #000000"&gt;); </span><span style="color: #0000ff"&gt;if</span> ( <span style="color: #008080"&gt;preg_match</span>('{<li>City : ([^<]*)</li>}i',<span style="color: #800080"&gt;$content</span>,<span style="color: #800080"&gt;$regs</span><span style="color: #000000"&gt;) ) { </span><span style="color: #800080"&gt;$city</span> = <span style="color: #800080"&gt;$regs</span>[1<span style="color: #000000"&gt;]; } </span><span style="color: #0000ff"&gt;if</span> ( <span style="color: #008080"&gt;preg_match</span>('{<li>State/Province : ([^<]*)</li>}i',<span style="color: #800080"&gt;$regs</span><span style="color: #000000"&gt;) ) { </span><span style="color: #800080"&gt;$state</span> = <span style="color: #800080"&gt;$regs</span>[1<span style="color: #000000"&gt;]; } </span><span style="color: #0000ff"&gt;if</span>( <span style="color: #800080"&gt;$city</span>!='' &amp;&amp; <span style="color: #800080"&gt;$state</span>!=''<span style="color: #000000"&gt; ){ </span><span style="color: #800080"&gt;$location</span> = <span style="color: #800080"&gt;$city</span> . ',' . <span style="color: #800080"&gt;$state</span><span style="color: #000000"&gt;; </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$location</span><span style="color: #000000"&gt;; }</span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;{ </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$default</span><span style="color: #000000"&gt;; } }</span></pre>

语法:

5. 获取 Web 页面的源代码

使用下面的函数,可以获取任意 Web 页面的 HTML 代码。

display_sourcecode( = ( = "" ( => .= "Line #{} : " . () . "
n"

语法:

6. 计算喜欢你的 Facebook 页面的用户

fb_fan_count( = json_decode(("https://graph.facebook.com/". = ->

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读