Heroku php slim 应用程序 - 500 内部服务器错误
Heroku php slim app - 500 internal server error
我正在尝试使用 php 和 heroku 创建一个 REST api。
我在我的项目中安装了 slim,并创建了一个自定义 class 以从 api 服务获取所需的数据。我已经将源代码部署到 heroku,但是当我尝试到达我创建的端点时遇到问题,服务器会给我一个 500 错误。从 heroku 记录的错误如下:
2020-10-08T12:36:48.089896+00:00 app[web.1]: [08-Oct-2020 12:36:48] WARNING: [pool www] child 155, script '/app/index.php' (request: "GET /index.php") executing too slow (3.632728 sec), logging
2020-10-08T12:36:48.090094+00:00 app[web.1]:
2020-10-08T12:36:48.090180+00:00 app[web.1]: [08-Oct-2020 12:36:48] [pool www] pid 155
2020-10-08T12:36:48.090246+00:00 app[web.1]: script_filename = /app/index.php
2020-10-08T12:36:48.090377+00:00 app[web.1]: [0x00007fefd90167e0] curl_exec() /app/src/ExtensionDataProvider.php:39
2020-10-08T12:36:48.090508+00:00 app[web.1]: [0x00007fefd9016750] createBlobURI() /app/src/ExtensionDataProvider.php:50
2020-10-08T12:36:48.090658+00:00 app[web.1]: [0x00007fefd90166e0] fetchFeaturedImageData() /app/src/ExtensionDataProvider.php:27
2020-10-08T12:36:48.090762+00:00 app[web.1]: [0x00007fefd9016650] fetchPostsData() /app/index.php:17
2020-10-08T12:36:48.090942+00:00 app[web.1]: [0x00007fefd90165b0] {closure}() /app/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php:43
2020-10-08T12:36:48.091084+00:00 app[web.1]: [0x00007fefd90164e0] __invoke() /app/vendor/slim/slim/Slim/Routing/Route.php:381
2020-10-08T12:36:48.091234+00:00 app[web.1]: [0x00007fefd9016430] handle() /app/vendor/slim/slim/Slim/MiddlewareDispatcher.php:81
2020-10-08T12:36:48.091383+00:00 app[web.1]: [0x00007fefd90163c0] handle() /app/vendor/slim/slim/Slim/MiddlewareDispatcher.php:81
2020-10-08T12:36:48.091524+00:00 app[web.1]: [0x00007fefd9016350] handle() /app/vendor/slim/slim/Slim/Routing/Route.php:341
2020-10-08T12:36:48.091667+00:00 app[web.1]: [0x00007fefd90162e0] run() /app/vendor/slim/slim/Slim/Routing/RouteRunner.php:84
2020-10-08T12:36:48.091816+00:00 app[web.1]: [0x00007fefd9016240] handle() /app/vendor/slim/slim/Slim/MiddlewareDispatcher.php:81
2020-10-08T12:36:48.091940+00:00 app[web.1]: [0x00007fefd90161d0] handle() /app/vendor/slim/slim/Slim/App.php:215
2020-10-08T12:36:48.092088+00:00 app[web.1]: [0x00007fefd9016130] handle() /app/vendor/slim/slim/Slim/App.php:199
2020-10-08T12:36:48.092172+00:00 app[web.1]: [0x00007fefd9016090] run() /app/index.php:23
2020-10-08T12:36:56.279490+00:00 app[web.1]: [08-Oct-2020 12:36:56 UTC] PHP Fatal error: Uncaught TypeError: fwrite() expects parameter 2 to be string, array given in /app/vendor/slim/psr7/src/Stream.php:357
2020-10-08T12:36:56.279507+00:00 app[web.1]: Stack trace:
2020-10-08T12:36:56.279508+00:00 app[web.1]: #0 /app/vendor/slim/psr7/src/Stream.php(357): fwrite()
2020-10-08T12:36:56.279509+00:00 app[web.1]: #1 /app/index.php(19): Slim\Psr7\Stream->write()
2020-10-08T12:36:56.279644+00:00 app[web.1]: #2 /app/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php(43): {closure}()
2020-10-08T12:36:56.279943+00:00 app[web.1]: #3 /app/vendor/slim/slim/Slim/Routing/Route.php(381): Slim\Handlers\Strategies\RequestResponse->__invoke()
2020-10-08T12:36:56.280133+00:00 app[web.1]: #4 /app/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Slim\Routing\Route->handle()
2020-10-08T12:36:56.280325+00:00 app[web.1]: #5 /app/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Slim\MiddlewareDispatcher->handle()
2020-10-08T12:36:56.280511+00:00 app[web.1]: #6 /app/vendor/slim/slim/Slim/Routing/Route.php(341): Slim\MiddlewareDispatcher->handle()
2020-10-08T12:36:56.280696+00:00 app[web.1]: #7 /app/vendor/slim/slim/Slim/Routing/RouteRunner.php(84): Slim\Routing\Route->run()
2020-10-08T12:36:56.280889+00:00 app[web.1]: #8 /app/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Slim\Routing\RouteRunner->handle()
2020-10-08T12:36:56.281053+00:00 app[web.1]: #9 /app/vendor/slim/slim/Slim/App.php(215): Slim\MiddlewareDispatcher->handle()
2020-10-08T12:36:56.281277+00:00 app[web.1]: #10 /app/vendor/slim/slim/Slim/App.php(199): Slim\App->hand in /app/vendor/slim/psr7/src/Stream.php on line 357
这是我自定义的代码class
<?php
/*
* @package Extension Data Provider
* @version 1.0.0
*/
namespace Browser;
class ExtensionDataProvider {
private static $blob;
private static $ch;
private static $data;
private static $item;
private static $output;
private static $response;
public static function fetchPostsData() : Array
{
self::$ch = curl_init('https://example.com/wp-json/wp/v2/posts?per_page=20');
curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER, true );
self::$response = curl_exec(self::$ch);
curl_close(self::$ch);
self::$data = json_decode(self::$response, true);
self::$output = [];
foreach( self::$data as $val ){
self::$blob = self::fetchFeaturedImageData($val['_links']['wp:featuredmedia'][0]['href']);
self::$item = [ $val['title']['rendered'], $val['link'], self::$blob ];
array_push( self::$output, self::$item );
}
return self::$output;
}
private static function createBlobURI( String $imageData ) : String
{
self::$data = json_decode($imageData, true);
self::$ch = curl_init(self::$data['source_url']);
curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER,true);
$image = curl_exec(self::$ch);
curl_close(self::$ch);
return $image;
}
private static function fetchFeaturedImageData( String $link ) : String
{
self::$ch = curl_init($link);
curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER, true);
self::$data = curl_exec(self::$ch);
curl_close(self::$ch);
return self::createBlobURI(self::$data);
}
}
?>
这是将路由请求的索引文件的代码
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use Browser\ExtensionDataProvider;
require __DIR__ . '/vendor/autoload.php';
$app = AppFactory::create();
$app->get('/', function(Request $request, Response $response, $args){
$response->getBody()->write("Hello world!");
return $response;
});
$app->get('/api/v1/images/', function(Request $request, Response $response, $args){
$data = ExtensionDataProvider::fetchPostsData();
json_encode($data, JSON_UNESCAPED_SLASHES);
$response->getBody()->write($data);
return $response->withHeader('Content-Type', 'application/json');
});
$app->run();
?>
我不确定如何解决,但我认为问题出在 slim 框架上?谁能帮帮我?
PHP Fatal error: Uncaught TypeError: fwrite() expects parameter 2 to be string, array given in /app/vendor/slim/psr7/src/Stream.php:357
这个方法的结果是一个数组...
$data = ExtensionDataProvider::fetchPostsData();
要修复它,试试这个:
$app->get('/api/v1/images/', function(Request $request, Response $response, $args){
$data = ExtensionDataProvider::fetchPostsData();
$json = json_encode($data, JSON_UNESCAPED_SLASHES);
$response->getBody()->write($json);
return $response->withHeader('Content-Type', 'application/json');
});
我正在尝试使用 php 和 heroku 创建一个 REST api。 我在我的项目中安装了 slim,并创建了一个自定义 class 以从 api 服务获取所需的数据。我已经将源代码部署到 heroku,但是当我尝试到达我创建的端点时遇到问题,服务器会给我一个 500 错误。从 heroku 记录的错误如下:
2020-10-08T12:36:48.089896+00:00 app[web.1]: [08-Oct-2020 12:36:48] WARNING: [pool www] child 155, script '/app/index.php' (request: "GET /index.php") executing too slow (3.632728 sec), logging
2020-10-08T12:36:48.090094+00:00 app[web.1]:
2020-10-08T12:36:48.090180+00:00 app[web.1]: [08-Oct-2020 12:36:48] [pool www] pid 155
2020-10-08T12:36:48.090246+00:00 app[web.1]: script_filename = /app/index.php
2020-10-08T12:36:48.090377+00:00 app[web.1]: [0x00007fefd90167e0] curl_exec() /app/src/ExtensionDataProvider.php:39
2020-10-08T12:36:48.090508+00:00 app[web.1]: [0x00007fefd9016750] createBlobURI() /app/src/ExtensionDataProvider.php:50
2020-10-08T12:36:48.090658+00:00 app[web.1]: [0x00007fefd90166e0] fetchFeaturedImageData() /app/src/ExtensionDataProvider.php:27
2020-10-08T12:36:48.090762+00:00 app[web.1]: [0x00007fefd9016650] fetchPostsData() /app/index.php:17
2020-10-08T12:36:48.090942+00:00 app[web.1]: [0x00007fefd90165b0] {closure}() /app/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php:43
2020-10-08T12:36:48.091084+00:00 app[web.1]: [0x00007fefd90164e0] __invoke() /app/vendor/slim/slim/Slim/Routing/Route.php:381
2020-10-08T12:36:48.091234+00:00 app[web.1]: [0x00007fefd9016430] handle() /app/vendor/slim/slim/Slim/MiddlewareDispatcher.php:81
2020-10-08T12:36:48.091383+00:00 app[web.1]: [0x00007fefd90163c0] handle() /app/vendor/slim/slim/Slim/MiddlewareDispatcher.php:81
2020-10-08T12:36:48.091524+00:00 app[web.1]: [0x00007fefd9016350] handle() /app/vendor/slim/slim/Slim/Routing/Route.php:341
2020-10-08T12:36:48.091667+00:00 app[web.1]: [0x00007fefd90162e0] run() /app/vendor/slim/slim/Slim/Routing/RouteRunner.php:84
2020-10-08T12:36:48.091816+00:00 app[web.1]: [0x00007fefd9016240] handle() /app/vendor/slim/slim/Slim/MiddlewareDispatcher.php:81
2020-10-08T12:36:48.091940+00:00 app[web.1]: [0x00007fefd90161d0] handle() /app/vendor/slim/slim/Slim/App.php:215
2020-10-08T12:36:48.092088+00:00 app[web.1]: [0x00007fefd9016130] handle() /app/vendor/slim/slim/Slim/App.php:199
2020-10-08T12:36:48.092172+00:00 app[web.1]: [0x00007fefd9016090] run() /app/index.php:23
2020-10-08T12:36:56.279490+00:00 app[web.1]: [08-Oct-2020 12:36:56 UTC] PHP Fatal error: Uncaught TypeError: fwrite() expects parameter 2 to be string, array given in /app/vendor/slim/psr7/src/Stream.php:357
2020-10-08T12:36:56.279507+00:00 app[web.1]: Stack trace:
2020-10-08T12:36:56.279508+00:00 app[web.1]: #0 /app/vendor/slim/psr7/src/Stream.php(357): fwrite()
2020-10-08T12:36:56.279509+00:00 app[web.1]: #1 /app/index.php(19): Slim\Psr7\Stream->write()
2020-10-08T12:36:56.279644+00:00 app[web.1]: #2 /app/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php(43): {closure}()
2020-10-08T12:36:56.279943+00:00 app[web.1]: #3 /app/vendor/slim/slim/Slim/Routing/Route.php(381): Slim\Handlers\Strategies\RequestResponse->__invoke()
2020-10-08T12:36:56.280133+00:00 app[web.1]: #4 /app/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Slim\Routing\Route->handle()
2020-10-08T12:36:56.280325+00:00 app[web.1]: #5 /app/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Slim\MiddlewareDispatcher->handle()
2020-10-08T12:36:56.280511+00:00 app[web.1]: #6 /app/vendor/slim/slim/Slim/Routing/Route.php(341): Slim\MiddlewareDispatcher->handle()
2020-10-08T12:36:56.280696+00:00 app[web.1]: #7 /app/vendor/slim/slim/Slim/Routing/RouteRunner.php(84): Slim\Routing\Route->run()
2020-10-08T12:36:56.280889+00:00 app[web.1]: #8 /app/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Slim\Routing\RouteRunner->handle()
2020-10-08T12:36:56.281053+00:00 app[web.1]: #9 /app/vendor/slim/slim/Slim/App.php(215): Slim\MiddlewareDispatcher->handle()
2020-10-08T12:36:56.281277+00:00 app[web.1]: #10 /app/vendor/slim/slim/Slim/App.php(199): Slim\App->hand in /app/vendor/slim/psr7/src/Stream.php on line 357
这是我自定义的代码class
<?php
/*
* @package Extension Data Provider
* @version 1.0.0
*/
namespace Browser;
class ExtensionDataProvider {
private static $blob;
private static $ch;
private static $data;
private static $item;
private static $output;
private static $response;
public static function fetchPostsData() : Array
{
self::$ch = curl_init('https://example.com/wp-json/wp/v2/posts?per_page=20');
curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER, true );
self::$response = curl_exec(self::$ch);
curl_close(self::$ch);
self::$data = json_decode(self::$response, true);
self::$output = [];
foreach( self::$data as $val ){
self::$blob = self::fetchFeaturedImageData($val['_links']['wp:featuredmedia'][0]['href']);
self::$item = [ $val['title']['rendered'], $val['link'], self::$blob ];
array_push( self::$output, self::$item );
}
return self::$output;
}
private static function createBlobURI( String $imageData ) : String
{
self::$data = json_decode($imageData, true);
self::$ch = curl_init(self::$data['source_url']);
curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER,true);
$image = curl_exec(self::$ch);
curl_close(self::$ch);
return $image;
}
private static function fetchFeaturedImageData( String $link ) : String
{
self::$ch = curl_init($link);
curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER, true);
self::$data = curl_exec(self::$ch);
curl_close(self::$ch);
return self::createBlobURI(self::$data);
}
}
?>
这是将路由请求的索引文件的代码
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use Browser\ExtensionDataProvider;
require __DIR__ . '/vendor/autoload.php';
$app = AppFactory::create();
$app->get('/', function(Request $request, Response $response, $args){
$response->getBody()->write("Hello world!");
return $response;
});
$app->get('/api/v1/images/', function(Request $request, Response $response, $args){
$data = ExtensionDataProvider::fetchPostsData();
json_encode($data, JSON_UNESCAPED_SLASHES);
$response->getBody()->write($data);
return $response->withHeader('Content-Type', 'application/json');
});
$app->run();
?>
我不确定如何解决,但我认为问题出在 slim 框架上?谁能帮帮我?
PHP Fatal error: Uncaught TypeError: fwrite() expects parameter 2 to be string, array given in /app/vendor/slim/psr7/src/Stream.php:357
这个方法的结果是一个数组...
$data = ExtensionDataProvider::fetchPostsData();
要修复它,试试这个:
$app->get('/api/v1/images/', function(Request $request, Response $response, $args){
$data = ExtensionDataProvider::fetchPostsData();
$json = json_encode($data, JSON_UNESCAPED_SLASHES);
$response->getBody()->write($json);
return $response->withHeader('Content-Type', 'application/json');
});