简体   繁体   English

php imap函数与cronjob

[英]php imap functions with cronjob

I have a php script with imap functions. 我有一个带imap函数的php脚本。 All is fine when I call the php file from browser, but when it run with cronjob the following error: 当我从浏览器调用php文件时,一切都很好,但是当它与cronjob一起运行时,出现以下错误:

    X-Powered-By: PHP/5.3.27
    Set-Cookie: bgm=31dc5ff02b6a3d1614a9dd1c39321fd0; path=/
    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    Pragma: no-cache
    Content-type: text/html

    <br />
    <b>Warning</b>:  imap_open() [<a href='function.imap-open'>function.imap-open</a>]: Couldn't open stream {"hostname":993/imap/notls/ssl} in <b>/"path"/cron.php</b> on line <b>173</b><br /> 
    <br/><br/><span style='color:red'></span><pre>Certificate failure for "hostname": unable to get local issuer certificate: /C=US/O=GeoTrust Inc./CN=GeoTrust Global CA</pre><br/><br/><br />

OS: CentOS 5.9 The cronjob was created with Cpanel: /usr/bin/php file.php. 操作系统:CentOS 5.9 cronjob是使用Cpanel创建的:/ usr / bin / php file.php。 The imap_last_error() give the following error: Certificate failure for "hostname" : unable to get local issuer certificate: But when i call the cron.php from any browser that certificate failure doesn't come and all works fine. imap_last_error()给出以下错误: “主机名”的证书失败:无法获取本地颁发者证书:但是,当我从任何浏览器调用cron.php时,证书失败不会发生,并且一切正常。

OpenSSL takes the CA bundle file location / CA directory location either as a parameter (command line or library method call) or from the environment variables SSL_CERT_FILE / SSL_CERT_DIR. OpenSSL将CA软件包文件位置/ CA目录位置作为参数(命令行或库方法调用)或从环境变量SSL_CERT_FILE / SSL_CERT_DIR中获取。

My guess is in this case the Apache environment had one of these set correctly and the cron environment did not. 我的猜测是,在这种情况下,Apache环境正确设置了其中之一,而cron环境则没有。 Two solutions: 两种解决方案:

  1. set one of these env variables at the top of the crontab: 在crontab的顶部设置以下环境变量之一:

SSL_CERT_FILE="/path/to/ca-bundle.crt"

  1. set the env variable in the php script before opening imap connection: 在打开imap连接之前,在php脚本中设置env变量:

putenv('SSL_CERT_FILE=/usr/share/ssl/certs/ca-bundle.crt');

Thank you to @Daniel-Roethlisberger for inspiring me to find this second solution based on his solution in Ruby: https://stackoverflow.com/a/14797635/2294879 感谢@ Daniel-Roethlisberger启发我根据他在Ruby中的解决方案找到第二个解决方案: https : //stackoverflow.com/a/14797635/2294879

I think also it must be possible to specify a default bundle location in the build of the imap program/library that PHP uses (because my imap connection used to work fine before applying the above), but this is surely more hassle. 我认为在PHP使用的imap程序/库的构建中也必须指定默认的包位置(因为在应用上述操作之前,我的imap连接曾经可以正常工作),但这肯定比较麻烦。

It seems as if PHP is unable to verify the certificate. 似乎PHP无法验证证书。 Can you check whether it works when adding /novalidate-cert to the resource URL? 将/ novalidate-cert添加到资源URL时,您能否检查其是否有效?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM