gpt4 book ai didi

symfony - o嵌入 Symfony 3 实现

转载 作者:行者123 更新时间:2023-12-04 16:11:05 25 4
gpt4 key购买 nike

我想允许用户创建 oEmbed内容到我的网站。有很多关于如何实现它的信息,但没有关于创建 enpoint 的信息。

  1. 快速提问:是否有一些现成的 php 代码示例可用于创建(返回)oEmbed 对第三方网站的响应?

  2. 如果没有,如何创建?前 youtube 链接如下所示:

    http://www.youtube.com/oembed?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DiwGFalTRHDA

    响应是:

    {
    "html": "<iframe width=\"459\" height=\"344\" src=\"https://www.youtube.com/embed/iwGFalTRHDA?feature=oembed\" frameborder=\"0\" allowfullscreen></iframe>",
    "thumbnail_height": 360,
    "thumbnail_width": 480,
    "provider_name": "YouTube",
    "author_url": "https://www.youtube.com/user/KamoKatt",
    "thumbnail_url": "https://i.ytimg.com/vi/iwGFalTRHDA/hqdefault.jpg",
    "author_name": "KamoKatt",
    "provider_url": "https://www.youtube.com/",
    "type": "video",
    "version": "1.0",
    "width": 459,
    "title": "Trololo",
    "height": 344
    }

    我的问题是:他们怎么知道这是哪个视频?他们正在使用正则表达式来解析 videoID

  3. 这种请求的“最佳做法”是什么?应该将其创建为 Controller、Service、Provider 还是如何创建?

最佳答案

最后我像这样创建了它:

  1. 我创建了一个服务:

namespace AppBundle\Service;

use AppBundle\Entity\PlaceMarker;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception as Ex;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;


class EmbedService
{

CONST THUMBNAIL_WIDTH = 400;
CONST THUMBNAIL_HEIGHT = 300;
CONST HTML_WIDTH = 459;
CONST HTML_HEIGHT = 344;

/**
* @var string
*/
protected $baseUrl;

/**
* @var array
*/
private $params = [];

/**
* @var EntityManager
*/
private $entityManager;

/** @var \Twig_Environment */
private $twig;

public function __construct($baseUrl, EntityManager $entityManager, \Twig_Environment $twig)
{
$this->baseUrl = strtolower($baseUrl);
$this->entityManager = $entityManager;
$this->twig = $twig;
}

/**
*
* @param Request $request
* @param null $format
* @throws \Exception
*/
public function handleRequest(Request $request, $format = null)
{
$this->params = [];

// $baseUrl = $request->getSchemeAndHttpHost();

/** handle url parameter */
$this->params['url'] = $request->query->get('url');
if (!$this->params['url']) {
throw new Ex\BadRequestHttpException('A URL parameter must be provided');
}

/** check if url matches site url */
$this->params['path'] = substr($request->query->get('url'), strlen($this->baseUrl));
if (substr(strtolower($request->query->get('url')), 0, strlen($this->baseUrl)) !== strtolower($this->baseUrl)) {
throw new Ex\BadRequestHttpException('This is not a valid URL parameter');
}

/** handle format parameters */
$this->params['format'] = $format;
if (null === $format) {
$this->params['format'] = $request->query->get('format', 'json');
}

if (!in_array($this->params['format'], ['json', 'xml'])) {
throw new \Exception('Disallowed format parameter \'' . $this->params['format'] . '\'');
}

$maxWidth = $request->query->get('maxwidth', null);
if (is_numeric($maxWidth) || is_null($maxWidth)) {
$this->params['maxWidth'] = $maxWidth;
}

$maxHeight = $request->query->get('maxheight', null);
if (is_numeric($maxHeight) || is_null($maxHeight)) {
$this->params['maxHeight'] = $maxHeight;
}

$this->params['attr'] = $this->matchRoute($this->params['path']);
}

public function matchRoute($path)
{

$route1 = new Route('/place-marker/{id}/show', ['controller' => 'PlaceMarkerController']);
$routes = new RouteCollection();
$routes->add('place-marker', $route1);

$context = new RequestContext('/');

$matcher = new UrlMatcher($routes, $context);

try {
return $matcher->match($path);
} catch (\Exception $ex) {
throw new Ex\NotFoundHttpException('No implemented!');
}
}

/**
* @return array
*/
public function create()
{
if (!$this->params) {
throw new Exception('Unable to create oEmbed. Did you use handleRequest() first?');
}

$oEmbed = [
'version' => '1.0',
'provider_url' => $this->baseUrl,
'provider_name' => 'Pixdor.com'
];

switch ($this->params['attr']['_route']) {
case 'place-marker':

/**
* @var PlaceMarker $pMarker
*/
$pMarker = $this->entityManager->getRepository(PlaceMarker::class)->find($this->params['attr']['id']);

$oEmbed['title'] = $pMarker->getName();
$oEmbed['url'] = $pMarker->getUrl();
$oEmbed['description'] = $pMarker->getDescription();
$oEmbed['caption'] = $pMarker->getCaption();

$oEmbed['html'] = $this->twig->render(':place_marker:embed.html.twig');
$oEmbed['width'] = self::HTML_WIDTH;
$oEmbed['height'] = self::HTML_HEIGHT;

$oEmbed['rich'] = 'photo';
break;
}

$oEmbed['thumbnail_width'] = isset($this->params['maxWidth']) ? $this->params['maxWidth'] : self::THUMBNAIL_WIDTH;
$oEmbed['thumbnail_height'] = isset($this->params['thumbnail_height']) ? $this->params['thumbnail_height'] : self::THUMBNAIL_HEIGHT;

$oEmbed['thumbnail_url'] = sprintf('http://placehold.it/%dx%d', $oEmbed['thumbnail_width'], $oEmbed['thumbnail_height']);

return $oEmbed;
}

/**
* Returns format type (json|xml)
* @return string
*/
public function getFormat()
{
if (!$this->params) {
throw new Exception('Unknown format type. Did you use handleRequest() first?');
}

return $this->params['format'];
}

}

服务.yml

app.embed:
class: AppBundle\Service\EmbedService
arguments: ["%base_url%", "@doctrine.orm.entity_manager", "@twig"]

参数.yml

base_url: "http://project.local"

接下来在 Controller 中:

/**
* @Route("/oembed.{_format}",
* name="embed",
* defaults={"_format": null}
* )
* @param Request $request
* @param $_format
* @return \Symfony\Component\HttpFoundation\Response
* @throws \Exception
*/
public function generateAction(Request $request, $_format)
{

$oEmbed = $this->get('app.embed');
$oEmbed->handleRequest($request, $_format);

$view = View::create()
->setFormat($oEmbed->getFormat())
->setStatusCode(200)
->setData($oEmbed->create());

return $this->handleView($view);
}

你可以这样调用它: http://project.local/oembed?url=http://project.local/place-marker/4/show

关于symfony - o嵌入 Symfony 3 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41223607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com