简体   繁体   中英

Why is the file path for stream_context_set_option screwy?

I have some PHP scripts in the following directory structure.

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

In NotificationService.php , I have the following line in my "notifyUser()" function:

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

However, when I include NotificationService.php in my Login.php script and try to call notifyUser(), it gives me this error:

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]

However, if I call notifyUser() from a script in the same directory as NotificationService.php , there is no error. Why does the bolded line above seem to be affected by the script calling the function, rather than the location of the script that the function is defined in?

Relative paths are resolved based on the include_path setting; this typically includes the current working directory that's established at the start of each request.

If you want to make sure the path is always taken relative to the current script that's being run:

// NotificationService.php

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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