我对php和wordpress比较陌生。我正在尝试制作一个自定义窗口小部件,以在边栏中显示我的存储桶中的所有文件夹和文件。我已经在小部件外部测试了代码,但是当我将其放在类对象中并尝试将其与wordpress中的elementor一起拖入页面时,出现500错误,并且无法更新页面。我觉得自己似乎遗漏了一些非常明显的东西,但是今天我一直在挠头。
这是使用aws php sdk在质量保证服务器上运行的。
<?php
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
class S3_Widget extends WP_Widget {
// php classnames and widget name/description added
function __construct() {
$widget_options = array(
'classname' => 's3_widget',
'description' => 'Add a formatted list of s3 folders & files to the sidebar.'
);
parent::__construct(
's3_widget',
'S3 Widget',
$widget_options
);
}
// create the widget output
function widget( $args, $instance ) {
require_once 'vendor/aws.phar';
$bucket = 'gravityforms-bucket';
// Instantiate the client.
$s3 = new S3Client([
'profile' => 'formsS3',
'version' => 'latest',
'region' => 'us-west-2'
]);
echo "<ul>";
// Use the high-level iterators (returns ALL of your objects).
try {
$results = $s3->getPaginator('ListObjects', [
'Bucket' => $bucket
]);
foreach ($results as $result) {
foreach ($result['Contents'] as $object) {
echo "<li>" . $object['Key'] . PHP_EOL . "</li>";
}
}
} catch (S3Exception $e) {
echo $e->getMessage();
} catch (Exception $e) {
echo "<pre>errorMessage::"; echo print_r($e->getMessage(), true); echo "</pre>";
}
echo "</ul>";
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
return $instance;
}
}
// register the widget
function s3_register_widgets() {
register_widget( 'S3_Widget' );
}
add_action( 'widgets_init', 's3_register_widgets' );
?>
答案 0 :(得分:0)
请参阅以下我的建议。
1-将S3功能与小部件分开。
制作一个可与S3一起使用的PHP类。此类必须返回S3存储桶对象的数组或列表。
测试PHP类。
2-尝试将S3类集成到小部件中。
https://docs.aws.amazon.com/AmazonS3/latest/dev/ListingObjectKeysUsingPHP.html
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
class S3_List{
public function s3_listing() {
$bucket = '*** Your Bucket Name ***';
// Instantiate the client.
$s3 = new S3Client([
'version' => 'latest',
'region' => 'us-east-1'
]);
// Use the high-level iterators (returns ALL of your objects).
try {
$results = $s3->getPaginator('ListObjects', [
'Bucket' => $bucket
]);
foreach ($results as $result) {
foreach ($result['Contents'] as $object) {
echo $object['Key'] . PHP_EOL;
}
}
} catch (S3Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
// Use the plain API (returns ONLY up to 1000 of your objects).
try {
$objects = $s3->listObjects([
'Bucket' => $bucket
]);
foreach ($objects['Contents'] as $object) {
echo $object['Key'] . PHP_EOL;
}
} catch (S3Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
}
}
if ( !function_exists( 'get_s3_bucket' ) ) {
function get_s3_bucket() {
$s3_list = new s3_listing();
return $s3_list->s3_listing();
}
}
关于。 埃德。