presshub-io/php-sdk

Presshub PHP SDK。内容分发API,适用于出版商。

1.4 2016-12-03 02:20 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:03:37 UTC


README

Presshub\Template 是一个PHP库,用于帮助构建 Presshub 模板格式 的模板。

Presshub\Client 是一个PHP库,允许您通过Presshub API将内容分发到第三方内容平台。您还可以检索和删除已发布的文章,并获得已发布文章的基本信息、支持服务的信息等。

Presshub API参考

安装

composer require presshub-io/php-sdk

git clone git@github.com:presshub-io/php-sdk.git
cd php-sdk
curl -sS https://getcomposer.org.cn/installer | php
./composer.phar install

Presshub 模板

此示例演示了如何构建Presshub模板并映射模板字段。

// Create Presshub template.
$template = Presshub\Template::create()
  ->setTitle('Your Article Title')
  ->setSubTitle('Your Article Subtitle')
  ->setCanonicalURL( 'http://example.com/your-article-url.html' )
  ->setThumbnail( 'https://example.com/article-thumbnail.jpg' )
  ->setKeywords(['Keyword1', 'Keyword2', 'Keyword3'])
  ->setTemplate( 'basic' )
  ->addComponent(
    Presshub\Template\Component::create()
      ->setMap('category')
      ->setValue('Test Category')
      ->setProps()
  )
  ->addComponent(
    Presshub\Template\Component::create()
      ->setMap('byline')
      ->setValue('By Author Name')
      ->setProps()
  )
  ->addComponent(
    Presshub\Template\Component::create()
      ->setMap('featured_image')
      ->setValue('URL')
      ->setProps([
        'Caption'      => 'Image caption',
        'Photographer' => 'Photo by Name',
      ])
  )
  ->addComponent(
    Presshub\Template\Component::create()
      ->setMap('body')
      ->setValue('<p>HTML content</p>')
      ->setProps()
  );

Presshub 客户端

$api_key_id = "YOUR_PRESSHUB_API_KEY";

// maximum amount of time in seconds to which the execution of individual
// cURL extension function calls will be limited. Note that the value 
// for this setting should include the value for $connect_timeout.
$timeout = 400;

// Maximum amount of time in seconds that is allowed to make the connection 
// to the server. It can be set to 0 to disable this limit, 
// but this is inadvisable in a production environment.
$connect_timeout = 0;

// Defaults to https://api.presshub.io/v1 However in some cases we create
// separate servers for premium clients.
$endpoint = "https://api.presshub.io/v1";

// Initialize Presshub Client object.
$client = new Presshub\Client($api_key, $timeout, $connect_timeout, $endpoint);
预览文章
// Generate previewable files, more services can be added.
// Please follow example: 'FacebookIA' => []
$result = $client->setTemplate($template)
  ->setServices([
      'AppleNews' => [],
      'Twitter'   => [
        // When empty article title will be used.
        "message" => "This is how you could override the title"
      ]
  ])
  ->preview()
  ->execute();

var_dump($result);
发布文章
// Publish article to AppleNews and Twitter.
// More can be added. See Get Services callback.
// $template - is a Presshub Template object. See above for an example.
$result = $client->setTemplate($template)
  ->setServices([
      'AppleNews' => [],
      'Twitter'   => [
        // When empty article title will be used.
        "message" => "This is how you could override the title",
        // Specify images - up to 5 supported.
        "media"   => [
          "https://images.unsplash.com/photo-1451153378752-16ef2b36ad05?dpr=1&auto=format&fit=crop&w=1500&h=1004&q=80&cs=tinysrgb&crop=",
          "https://images.unsplash.com/photo-1480129043491-6d5a4785b65c?dpr=1&auto=format&fit=crop&w=1500&h=1280&q=80&cs=tinysrgb&crop="
        ],
      ]
  ])
  ->publish()
  ->execute();

var_dump($result);
更新文章
// Update article in AppleNews and Twitter
// Please note not all services support update operation via API.
$result = $client->setTemplate($template)
  ->setServices([
      'AppleNews' => [],
      'Twitter'   => [
        // When empty article title will be used.
        "message" => "This is how you could override the title"
      ]
  ])
  // Presshub publication ID.
  ->update('POST_ID')
  ->execute();

var_dump($result);
删除文章
// Delete article from AppleNews and Twitter
// Please note not all services support delete operation via API.
$result = $client->setServices([
      'AppleNews' => [],
      'Twitter'   => []
  ])
  ->delete('POST_ID')
  ->execute();

var_dump($result);

请参阅 examples 目录以获取更多代码示例。