[英]php - notification in stream_context_set_params() not called
I am using this code in php (Wordpress) to check for download progress: 我正在php(Wordpress)中使用此代码来检查下载进度:
// Create context
$context = stream_context_create();
stream_context_set_params( $context, [ 'notification' => 'my_stream_notification_callback' ] );
// Declare progress function
function my_stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
print_r(func_get_args());
}
// Call download
$wp_upload_dir = wp_upload_dir();
file_put_contents( $wp_upload_dir['basedir'] . '/contact.htm', fopen( 'http://php.net/contact', 'r' ), null, $context );
The code does download the file successfully in the /wp-content/uploads/
folder, but does not print any notification / progress. 该代码确实在
/wp-content/uploads/
文件夹中成功下载了文件,但是没有打印任何通知/进度。
I tried also by writing to error_log()
in the my_stream_notification_callback()
function but it does not write anything there and the debug.log
file is empty. 我还尝试通过在
my_stream_notification_callback()
函数中写入error_log()
来进行my_stream_notification_callback()
但该函数未在其中写入任何内容,并且debug.log
文件为空。 Which means the notification callback is not being called at all. 这意味着根本没有调用通知回调。
Anyone has any idea why this would happen? 任何人都知道为什么会发生这种情况吗?
Here is a question with similar code but different problem: Download files file_put_contents with progress Evidently, the code works for him after the callback function was fixed for a class/object method function. 这是一个具有类似代码但有不同问题的问题: 下载带有进度的文件file_put_contents显然,在为类/对象方法函数固定了回调函数之后,该代码对他有效。 While I am trying a simpler callback function that should definitely work.
当我尝试一个更简单的回调函数时,它肯定可以正常工作。
Any ideas? 有任何想法吗?
-- EDIT -- -编辑-
I found an example on php.net here: http://php.net/manual/en/function.stream-notification-callback.php 我在php.net上找到了一个示例: http : //php.net/manual/en/function.stream-notification-callback.php
I used that example and it seems to work. 我用了那个例子,它似乎起作用了。
See the code I used (a bit changed from the example): 请参阅我使用的代码(与示例稍有不同):
$ctx = stream_context_create();
stream_context_set_params( $ctx, [
"notification" => function ( $notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max ) {
print_r(func_get_args());
},
] );
file_get_contents( "http://php.net/contact", false, $ctx );
Now this code works and prints out the progress notifications: 现在,此代码可以正常工作并打印进度通知:
Does it mean that file_put_contents()
or fopen()
has problems with context/notifications that file_get_contents()
do not have? 这是否意味着
file_put_contents()
或fopen()
遇到了file_get_contents()
没有的上下文/通知问题?
--EDIT-- - 编辑 -
Here is the change what actually works: 这是实际起作用的更改:
file_put_contents( $wp_upload_dir['basedir'] . '/contact.htm', fopen( 'http://php.net/contact', 'r', null, $context ) );
Means, instead of applying $context to file_put_contents()
we apply it to fopen()
call and it works! 意思是,不是将$ context应用于
file_put_contents()
而是将其应用于fopen()
调用,它可以工作!
As given in the documentation at http://php.net/stream_context_set_params , the notification
callback is only triggered for ftp
and http
connections. 如http://php.net/stream_context_set_params上的文档中所述,
notification
回调仅针对ftp
和http
连接触发。 As you use it to write something to a local directory (through another protocol), it's pretty obvious why the callback is never called. 当您使用它(通过另一个协议)将内容写入本地目录时,很明显为什么从不调用回调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.