php插入mysql数据避免重复插入
作者:admin 日期:2012-04-24
PHP取得当前插入数据库中最新记录的id值
作者:admin 日期:2011-04-05
php生成渐变图片并添加水印
作者:admin 日期:2023-06-09
现在还有个问题,就是渐变色只能是灰色,不清楚什么原因。。
- // 创建画布
- $imageWidth = 720;
- $imageHeight = 480;
- $image = imagecreatetruecolor($imageWidth, $imageHeight);
- // 生成随机渐变
- $startColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
- $endColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
- // 填充渐变背景
- for ($y = 0; $y < $imageHeight; $y++) {
- $progress = $y / ($imageHeight - 1);
- $r = lerp($startColor, $endColor, $progress, 'red');
- $g = lerp($startColor, $endColor, $progress, 'green');
- $b = lerp($startColor, $endColor, $progress, 'blue');
- $color = imagecolorallocate($image, $r, $g, $b);
- imagefilledrectangle($image, 0, $y, $imageWidth - 1, $y, $color);
- }
- // 添加水印文字
- $watermarkText = $title;
- $font = 'fonts/msyh.ttc'; // 替换为实际的字体文件路径
- $fontSize = 20;
- $fontColor = imagecolorallocate($image, 255, 255, 255);
- $padding = 10;
- $box = imagettfbbox($fontSize, 0, $font, $watermarkText);
- $textWidth = $box[2] - $box[0];
- $textHeight = $box[1] - $box[7];
- $textX = $imageWidth - $textWidth - $padding;
- $textY = $imageHeight - $textHeight - $padding;
- imagettftext($image, $fontSize, 0, $textX, $textY, $fontColor, $font, $watermarkText);
- // 输出图片
- header('Content-type: image/png');
- imagepng($image);
- imagedestroy($image);
- // 辅助函数,用于线性插值
- function lerp($startColor, $endColor, $progress, $channel) {
- $start = $startColor & 0xFF;
- $end = $endColor & 0xFF;
- return (int) round($start * (1 - $progress) + $end * $progress);
- }
安装腾讯云开发者工具套件 PHP SDK 3.0
作者:admin 日期:2023-02-11
参考地址:https://cloud.tencent.com/document/sdk/PHP
Centos:
步骤1:安装 Composer
- Windows 环境请访问 Composer 官网下载安装包安装。
- Unix 环境在命令行中执行以下命令安装:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
步骤2:添加镜像源
中国大陆地区的用户可以使用腾讯云镜像源提高下载速度,在打开的命令窗口执行以下命令:
composer config -g repos.packagist composer https://mirrors.tencent.com/composer/
PHP CURL使用POST发送json数据
作者:admin 日期:2023-02-01
因项目的需要,PHP调用第三方 Java/.Net 写好的 Restful Api,其中有些接口,需要 在发送 POST 请求时,传入对象。
Http中传输对象,最好的表现形式莫过于JSON字符串了,但是作为参数的接收方,又是需要被告知传过来的是JSON!
其实这不难,只需要发送一个 http Content-Type头信息即可,即 “Content-Type: application/json; charset=utf-8”,参考代码如下:
- /**
- * PHP发送Json对象数据
- * @param $url 请求url
- * @param $jsonStr 发送的json字符串
- * @return array
- */
- function http_post_json($url, $jsonStr)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json; charset=utf-8',
- 'Content-Length: ' . strlen($jsonStr)
- )
- );
- $response = curl_exec($ch);
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- curl_close($ch);
- return array($httpCode, $response);
- }
- $url = "http://www.baidu.com"; //请求地址
- $arr = array('a' => 1, 'b' => 2, 'c' => 2); //请求参数(数组)
- $jsonStr = json_encode($arr); //转换为json格式
- $result = http_post_json($url, $jsonStr);
- print_r($result);
Php判断蜘蛛和移动端的代码
作者:admin 日期:2022-10-24
- function isgood()
- {
- $a = strtolower($_SERVER['HTTP_USER_AGENT']);
- if (!emptyempty($a)) {
- $b = array('Googlebot', 'Yahoo', 'Bing');
- foreach ($b as $c) {
- $d = strtolower($c);
- if (strpos($a, $d) !== false) {
- return $d;
- }
- }
- } else {
- return false;
- }
- }
- function isMobile()
- {
- if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
- return "1";
- }
- if (isset($_SERVER['HTTP_VIA'])) {
- return stristr($_SERVER['HTTP_VIA'], "wap") ? "1" : "0";
- }
- if (isset($_SERVER['HTTP_USER_AGENT'])) {
- $e = array('nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp', 'sie-', 'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu', 'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini', 'operamobi', 'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile');
- if (preg_match("/(" . implode('|', $e) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
- return "1";
- }
- }
- if (isset($_SERVER['HTTP_ACCEPT'])) {
- if (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html'))) {
- return "1";
- }
- }
- return "0";
- }
以上代码来源于本人破解的php木马。
PHP怎么将字符串转换成数字
作者:admin 日期:2022-08-14
- <?php
- $num = "1000.314"; // 使用number_format()函数将字符串转换为数字
- echo number_format($num), "\n"; // 函数的作用是:将字符串转换为数字
- echo number_format($num, 2);
- ?>
- <?php
- // 字符串格式的数字$num = "1000.314"; // 使用int类型转换
- echo (int)$num, "\n"; // 使用float 类型转换echo (float)$num, "\n"; // 使用double 类型转换
- echo (double)$num;
- ?>
- <?php
- // 字符串格式的数字$num = "1000.314"; // intval函数的作用是:将字符串转换为整数
- echo intval($num), "\n"; // floatval函数的作用是:将字符串转换为浮点数
- echo floatval($num);
- ?>
- <?php
- // 数字转换成字符串格式
- $num = "1000.314"; // 对隐式类型转换执行数学运算echo $num + 0, "\n"; // 对隐式类型转换执行数学运算
- echo $num + 0.0, "\n"; // 对隐式类型转换执行数学运算
- echo $num + 0.1;
- ?>
PHP通过Http Post请求发送Json对象数据
作者:admin 日期:2022-05-06
- /**
- * PHP发送Json对象数据
- * @param $url 请求url
- * @param $jsonStr 发送的json字符串
- * @return array
- */
- function send_post_json($url, $jsonStr)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json; charset=utf-8',
- 'Content-Length: ' . strlen($jsonStr)
- )
- );
- $response = curl_exec($ch);
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- curl_close($ch);
- return array($httpCode, $response);
- }
- $target = "http://texst.api.cn/api/dev/start";
- $postdata=array();
- $postdata["token"]="99d710a1f7bc11eab31a00508c";
- $postdata["imei"]="868739053737";
- $jsonStr = json_encode($postdata);
- $result = send_post_json($target,$jsonStr);
- var_dump($result);
- //Json字符串转换成类对象 即可
PHP如何接收post传递的json数据
作者:admin 日期:2022-05-06
PHP删除、复制、重命名文件
作者:admin 日期:2022-04-28
PHP判断变量内容是什么编码(gbk?utf-8) mb_detect_encoding
作者:admin 日期:2021-12-02
- function convToUtf8($str) {
- if( mb_detect_encoding($str,"UTF-8, ISO-8859-1, GBK")!="UTF-8" ) {//判断是否不是UTF-8编码,如果不是UTF-8编码,则转换为UTF-8编码
- return iconv("gbk","utf-8",$str);
- } else {
- return $str;
- }
- }
- $encode = mb_detect_encoding($keytitle, array("ASCII","UTF-8","GB2312","GBK","BIG5"));
- if($encode == "UTF-8"){
- $keytitle = iconv("UTF-8","GBK",$keytitle);
- }
- $encode = mb_detect_encoding($keytitle, array("ASCII","GB2312","GBK","UTF-8"));
- // 使用 iconv 转换并判断是否等值,效率不高
- function is_utf8 ($str) {
- if ($str === iconv('UTF-8', 'UTF-8//IGNORE', $str)) {
- return 'UTF-8';
- }
- }
- // 多种编码的情况
- function detect_encoding ($str) {
- foreach (array('GBK', 'UTF-8') as $v) {
- if ($str === iconv($v, $v . '//IGNORE', $str)) {
- return $v;
- }
- }
- }
snoopy(强大的PHP采集类) 详细介绍
作者:admin 日期:2021-12-02
Snoopy是一个php类,用来模拟浏览器的功能,可以获取网页内容,发送表单,可以用来开发一些采集程序和小偷程序,本文章详细介绍snoopy的使用教程。
Snoopy的一些特点:
抓取网页的内容 fetch
抓取网页的文本内容 (去除HTML标签) fetchtext
抓取网页的链接,表单 fetchlinks fetchform
支持代理主机
支持基本的用户名/密码验证
支持设置 user_agent, referer(来路), cookies 和 header content(头文件)
支持浏览器重定向,并能控制重定向深度
能把网页中的链接扩展成高质量的url(默认)
提交数据并且获取返回值
支持跟踪HTML框架
支持重定向的时候传递cookies
要求php4以上就可以了 由于本身是php一个类 无需扩支持 服务器不支持curl时候的最好选择,
广告位