为什么stream_context_set_option的文件路径不正确


Why is the file path for stream_context_set_option screwy?

我在以下目录结构中有一些PHP脚本。

ROOT  
---COMMON  
------(Login.php)  
---APPSERVICE  
------(NotificationService.php)  
------NOTIFICATION_CERTIFICATES  
---------(ck.pem)

NotificationService.php中,我的"notifyUser()"函数中有以下行:

stream_context_set_option($ctx, 'ssl', 'local_cert', **'NOTIFICATION_CERTIFICATES/ck.pem'**);

但是,当我在Login.php脚本中包含NotificationService.php并尝试调用notifyUser()时,会出现以下错误:

stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure in [my file path]

但是,如果我从与NotificationService.php位于同一目录中的脚本中调用notifyUser(),则不会出现错误。为什么上面粗体的一行似乎受到调用函数的脚本的影响,而不是受到定义函数的脚本位置的影响?

基于include_path设置解析相对路径;这通常包括在每个请求开始时建立的当前工作目录。

如果您想确保路径始终相对于正在运行的当前脚本:

// NotificationService.php
$cert = __DIR__ . '/NOTIFICATION_CERTIFICATES/ck.pem';
stream_context_set_option($ctx, 'ssl', 'local_cert', $cert);