我的需求是 要拿到php生成的图片验证码的 base64 字符串,然后给app 显示 , 但是只有 file_get_content 能实现 , file_get_content 的地址又在当前项目 , 也就是 当前项目发起请求访问当前项目 , 但是我发现nginx超时了,应该是阻塞了吧 , 如何解决呢 ?
现在的问题是如何解决 windows 下 wnmp 当前php项目访问当前项目不阻塞的问题 。 (我在任务管理器看到 只有 php-cgi , 而没有 php-fpm)
我的需求是 要拿到php生成的图片验证码的 base64 字符串,然后给app 显示 , 但是只有 file_get_content 能实现 , file_get_content 的地址又在当前项目 , 也就是 当前项目发起请求访问当前项目 , 但是我发现nginx超时了,应该是阻塞了吧 , 如何解决呢 ?
现在的问题是如何解决 windows 下 wnmp 当前php项目访问当前项目不阻塞的问题 。 (我在任务管理器看到 只有 php-cgi , 而没有 php-fpm)
自己访问自己的服务器应该没问题,可以简单做个测试:定义一个简单的输出方法,在另外一个方法进行请求。
我感觉楼主的这种获取图片验证码的思路有些问题,在99%的情况中没必要做成这样。再想想有没有其他的方式去获取这个验证码
没太懂为啥要这么做,app 里直接调用生成验证码的 url (也就是你的 file_get_content 想要请求的自己的地址) 来显示图片不行吗,为啥要请求一下自己来获取图片然后再编码发给 APP.
如果非要这么做,你现在服务器上检查一下,直接使用 curl 命令,请求你的那个生成验证码的链接,看看能否正常响应.
更新:
这个问题环境无关啊,,我就想知道,为什么不能让APP里直接调用生成验证码的URL,非要间接的请求服务器,让服务器去请求这个服务器上的另一个URL然后拿到图,编码之后再返回给APP。 去掉中间的中转不行吗?
给你封装个函数
/**
* @param string $code 返回的验证码
* @param int $len 个数
* @param string $img_type 图片类型
* @return string 返回验证码base64
*/
function verificationBase64(&$code="",$len=4,$img_type="png")
{
$image = imagecreatetruecolor(100, 30);
$color = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 20, 20, $color);
$code = '';
for ($i = 0; $i <$len; $i++) {
$x = rand(5, 10) + $i * 100 / $len;
$y = rand(5, 15);
$font_size=8;
$data = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789';
$string = substr($data, rand(0, strlen($data)), 1);
$code .= $string;
$color = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));
imagestring($image, $font_size, $x, $y, $string, $color);
}
for ($i = 0; $i < 200; $i++) {
$pointColor = imagecolorallocate($image, rand(100, 255), rand(100, 255), rand(100, 255));
imagesetpixel($image, rand(0, 100), rand(0, 30), $pointColor);
}
for ($i = 0; $i < 2; $i++) {
$linePoint = imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
imageline($image, rand(10, 50), rand(10, 20), rand(80, 90), rand(15, 25), $linePoint);
}
$dir = dirname(__FILE__) . '/verifi_img';
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
//验证码图片保存路径,文件名称
$file_name = $dir . '/'.time().$code.'.'.$img_type;
imagepng($image,$file_name);
imagedestroy($image);
$img_base64 = '';
if (file_exists($file_name)) {
$app_img_file = $file_name; // 图片路径
$img_info = getimagesize($app_img_file); // 取得图片的大小,类型等
$fp = fopen($app_img_file, "r"); // 图片是否可读权限
if ($fp) {
$filesize = filesize($app_img_file);
$content = fread($fp, $filesize);
$file_content = chunk_split(base64_encode($content)); // base64编码
$img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;//合成图片的base64编码
}
fclose($fp);
unlink($file_name);
}
return $img_base64;
}
//调用
echo "验证码base64=========================<br>";
$code="";
echo verificationBase64($code,6);
echo "<br>====================================<br>";
echo "验证码是:".$code;
这个问题之前我也遇到过,在windows中,在项目中使用curl来抓取自己生成的图片或者接口的时候,php总是会超时.以下是我的解决方案.
第一种,将自己请求或抓取的接口直接在项目中使用。而不是通过抓取的形式。第二种,将php的运行模式改为fpm模式.可以使用xxfpm来将改为fpm模式.
2 回答1.3k 阅读✓ 已解决
1 回答988 阅读✓ 已解决
2 回答849 阅读✓ 已解决
1 回答1k 阅读✓ 已解决
2 回答906 阅读
1 回答891 阅读
1 回答820 阅读
不是很清楚你的业务过程,我的理解是 file_get_content 去拿一个文件,然后对其base64再给前端app显示,这个文件是一个url地址,然后这个url又刚好是本地服务器。
如果是这样的话,你可以去看一下nginx的日志,看具体错误原因,或者拿这个url直接在浏览器访问是否正常。