Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Upgrade to PHP ^8.1, add support for Symfony ^7.1 #39

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Test

on:
push:
branches:
- master
pull_request:
workflow_dispatch:

permissions:
contents: read

jobs:
test:
name: Test with PHP ${{ matrix.php-versions }}
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php-versions: [8.1, 8.2, 8.3] # TODO - Add 8.4 once all tools/dependencies support it

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}

- name: Install dependencies
uses: ramsey/composer-install@v3

- name: Run PHP-CS-Fixer
run: composer phpcs:check

- name: Run PHPStan
run: composer phpstan

- name: Run PHPUnit
run: composer test
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
.idea/
composer.lock
vendor/

###> friendsofphp/php-cs-fixer ###
/.php-cs-fixer.cache
###< friendsofphp/php-cs-fixer ###

###> phpunit/phpunit ###
/phpunit.xml
/.phpunit.cache
###< phpunit/phpunit ###
35 changes: 35 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* Copyright (c) Fusonic GmbH. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*/

declare(strict_types=1);

use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;

$finder = (new PhpCsFixer\Finder())
->in(__DIR__);

return (new PhpCsFixer\Config())
->setParallelConfig(ParallelConfigFactory::detect())
->setRules([
'@PHP81Migration' => true,
'@Symfony' => true,
'@Symfony:risky' => true,
'declare_strict_types' => true,
'header_comment' => [
'header' => 'Copyright (c) Fusonic GmbH. All rights reserved.'.\PHP_EOL.'Licensed under the MIT License. '.
'See LICENSE file in the project root for license information.',
'location' => 'after_open',
],
'no_useless_else' => true,
'no_useless_return' => true,
'php_unit_strict' => true,
'single_line_throw' => false,
'strict_comparison' => true,
'strict_param' => true,
])
->setFinder($finder)
->setRiskyAllowed(true);
17 changes: 0 additions & 17 deletions .scrutinizer.yml

This file was deleted.

6 changes: 0 additions & 6 deletions .travis.yml

This file was deleted.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add changes from 2.3 as seen in #36

File renamed without changes.
16 changes: 16 additions & 0 deletions CHANGELOG-3.x.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# CHANGELOG for 3.x

## 3.0.0

- Bumped the required PHP version from `^7.4 || ^8.0` to `^8.1`
- Bumped the compatible Symfony version from `^3.0 || ^4.0 || ^5.0 || ^6.0` to `^5.4 || ^6.4 || ^7.1`
- Upgraded PHPUnit from `^9.0` to `^11.4` and updated tests accordingly
- Added `composer test` script for running tests
- Installed `friendsofphp/php-cs-fixer` and applied Fusonic's code style
- Added `composer phpcs:check` script for validating code style
- Added `composer phpcs:fix` script for fixing code style violations
- Installed `phpstan/phpstan`, `phpstan/phpstan-deprecation-rules`, `phpstan/phpstan-phpunit` and
`phpstan/phpstan-strict-rules` and fixed reported errors
- Added `composer phpstan` script for validating code
- Updated documentation
- Switched to GitHub actions for automated testing
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2014 Fusonic GmbH (http://www.fusonic.net)
Copyright (c) 2014-2024 Fusonic GmbH (https://www.fusonic.net)

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
65 changes: 37 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,59 +1,64 @@
# fusonic/opengraph

[![Latest Stable Version](https://poser.pugx.org/fusonic/opengraph/v/stable)](https://packagist.org/packages/fusonic/opengraph)
[![Total Downloads](https://poser.pugx.org/fusonic/opengraph/downloads)](https://packagist.org/packages/fusonic/opengraph)
[![Build Status](https://api.travis-ci.org/fusonic/opengraph.svg)](https://travis-ci.org/fusonic/opengraph)
[![License](https://poser.pugx.org/fusonic/opengraph/license)](https://packagist.org/packages/fusonic/opengraph)
[![GitHub Release](https://img.shields.io/github/v/release/fusonic/opengraph)](https://github.com/fusonic/opengraph/releases/latest)
[![Packagist Downloads](https://img.shields.io/packagist/dt/fusonic/opengraph?color=blue)](https://packagist.org/packages/fusonic/opengraph)
[![Packagist License](https://img.shields.io/packagist/l/fusonic/opengraph)](https://github.com/fusonic/opengraph/blob/master/LICENSE)
[![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/fusonic/opengraph/test)](https://github.com/fusonic/opengraph/actions/workflows/test.yml)

A simple library to read Open Graph data from the web and generate HTML code to publish your own Open Graph objects. A fallback mode enables you to read data from websites that do not implement the Open Graph protocol.
A simple library to read Open Graph data from the web and generate HTML code to publish your own Open Graph objects. A
fallback mode enables you to read data from websites that do not implement the Open Graph protocol.

Using this library you can easily retrieve stuff like meta data, video information from YouTube or Vimeo or image information from Flickr without using site-specific APIs since they all implement the Open Graph protocol.
Using this library you can easily retrieve stuff like metadata, video information from YouTube or Vimeo or image
information from Flickr without using site-specific APIs since they all implement the Open Graph protocol.

See [ogp.me](http://ogp.me) for information on the Open Graph protocol.
See [ogp.me](https://ogp.me) for information on the Open Graph protocol.

## Requirements

* PHP 7.4+
* [fusonic/linq](https://github.com/fusonic/linq)
* [symfony/css-selector](https://github.com/symfony/CssSelector)
* [symfony/dom-crawler](https://github.com/symfony/DomCrawler)
* [psr/http-client](https://github.com/php-fig/http-client), [psr/http-factory](https://github.com/php-fig/http-factory) and compatible implementation such as [guzzle/guzzle](https://github.com/guzzle/guzzle)
* PHP 8.1+
* [symfony/css-selector](https://github.com/symfony/css-selector)
* [symfony/dom-crawler](https://github.com/symfony/dom-crawler)
* [psr/http-client](https://github.com/php-fig/http-client)
* [psr/http-factory](https://github.com/php-fig/http-factory)
* and compatible implementation such as [symfony/http-client](https://github.com/symfony/http-client)

## Installation

The most flexible installation method is using Composer:

``` bash
```bash
composer require fusonic/opengraph
```

Install composer and run install command:
``` bash
Install Composer and run the `install` command:
```bash
curl -s http://getcomposer.org/installer | php
php composer.phar install
```

Once installed, include vendor/autoload.php in your script.
Once installed, include `vendor/autoload.php` in your script.

``` php
require "vendor/autoload.php";
require 'vendor/autoload.php';
```

## Usage

### Retrieve Open Graph data from a URL

``` php
```php
<?php

use Fusonic\OpenGraph\Consumer;

$consumer = new Consumer($httpClient, $httpRequestFactory);
$object = $consumer->loadUrl("http://www.youtube.com/watch?v=P422jZg50X4");
$object = $consumer->loadUrl('https://www.youtube.com/watch?v=P422jZg50X4');

// Basic information of the object
echo "Title: " . $object->title; // Getting started with Facebook Open Graph
echo "Site name: " . $object->siteName; // YouTube
echo "Description: " . $object->description; // Originally recorded at the Facebook World ...
echo "Canonical URL: " . $object->url; // http://www.youtube.com/watch?v=P422jZg50X4
echo "Canonical URL: " . $object->url; // https://www.youtube.com/watch?v=P422jZg50X4

// Images
$image = $object->images[0];
Expand All @@ -63,7 +68,7 @@ echo "Image[0] width: " . $image->width; // null (May return width in pi

// Videos
$video = $object->videos[0];
echo "Video URL: " . $video->url; // http://www.youtube.com/v/P422jZg50X4?version=3&autohide=1
echo "Video URL: " . $video->url; // https://www.youtube.com/v/P422jZg50X4?version=3&autohide=1
echo "Video height: " . $video->height; // 1080
echo "Video width: " . $video->width; // 1920
echo "Video type: " . $video->type; // application/x-shockwave-flash
Expand All @@ -73,7 +78,9 @@ _There are some more properties but these are the basic and most commonly used o

### Publish own Open Graph data

``` php
```php
<?php

use Fusonic\OpenGraph\Elements\Image;
use Fusonic\OpenGraph\Elements\Video;
use Fusonic\OpenGraph\Publisher;
Expand All @@ -86,14 +93,14 @@ $object = new Website();
$object->title = "Getting started with Facebook Open Graph";
$object->siteName = "YouTube";
$object->description = "Originally recorded at the Facebook World ..."
$object->url = "http://www.youtube.com/watch?v=P422jZg50X4";
$object->url = "https://www.youtube.com/watch?v=P422jZg50X4";

// Images
$image = new Image("https://i1.ytimg.com/vi/P422jZg50X4/maxresdefault.jpg");
$object->images[] = $image;

// Videos
$video = new Video("http://www.youtube.com/v/P422jZg50X4?version=3&autohide=1");
$video = new Video("https://www.youtube.com/v/P422jZg50X4?version=3&autohide=1");
$video->height = 1080;
$video->width = 1920;
$video->type = "application/x-shockwave-flash";
Expand Down Expand Up @@ -125,18 +132,20 @@ _HTML code is formatted just for displaying purposes. You may choose between HTM

## Running tests

You can run the test suite by running `phpunit` from the command line.
You can run the test suite by running `composer test` from the command line.

## FAQ

**I don't get any information from a webpage, but Facebook shows information for the same URL. What do I do wrong?**

It seems that some pages (like Twitter) only publish OpenGraph information if Facebook's user agent string `facebookexternalhit/1.1` is used (see #28). So you should configure your PSR-18 client to use this user agent string:
It seems that some pages (like Twitter/X) only publish Open Graph information if Facebook's user agent string
`facebookexternalhit/1.1` is used (see [#28](https://github.com/fusonic/opengraph/issues/28)). So you should configure
your PSR-18 client to use this user agent string:

```php
$client = new Psr18Client(new NativeHttpClient([ "headers" => [ "User-Agent" => "facebookexternalhit/1.1" ] ]));
$client = new Psr18Client(new NativeHttpClient(['headers' => ['User-Agent' => 'facebookexternalhit/1.1']]));
```

## License

This library is licensed under the MIT license.
fusonic/opengraph is licensed under the MIT license. See [LICENSE](LICENSE) for more information.
5 changes: 5 additions & 0 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# UPGRADE FROM 2.2 TO 3.0

## Requirements
- Bumped the required PHP version from `^7.4 || ^8.0` to `^8.1`
- Bumped the compatible Symfony version from `^3.0 || ^4.0 || ^5.0 || ^6.0` to `^5.4 || ^6.4 || ^7.1`
36 changes: 26 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"name": "fusonic/opengraph",
"description": "PHP library for consuming and publishing Open Graph resources.",
"keywords": ["opengraph"],
"keywords": [
"opengraph"
],
"type": "library",
"homepage": "https://github.com/fusonic/fusonic-opengraph",
"homepage": "https://github.com/fusonic/opengraph",
"license": "MIT",
"authors": [
{
Expand All @@ -22,20 +24,34 @@
}
},
"require": {
"php": "^7.4|^8.0",
"php": "^8.1",
"ext-dom": "*",
"symfony/dom-crawler": "^3.0|^4.0|^5.0|^6.0",
"symfony/css-selector": "^3.0|^4.0|^5.0|^6.0",
"symfony/css-selector": "^5.4 || ^6.4 || ^7.1",
"symfony/dom-crawler": "^5.4 || ^6.4 || ^7.1",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"symfony/http-client": "^6.0",
"nyholm/psr7": "^1.2"
"friendsofphp/php-cs-fixer": "^3.65",
"nyholm/psr7": "^1.8",
"phpstan/phpstan": "^2.0",
"phpstan/phpstan-deprecation-rules": "^2.0",
"phpstan/phpstan-phpunit": "^2.0",
"phpstan/phpstan-strict-rules": "^2.0",
"phpunit/phpunit": "^10.5 || ^11.4",
"symfony/http-client": "^5.4 || ^6.4 || ^7.1"
},
"suggest": {
"symfony/http-client": "^5.0",
"nyholm/psr7": "^1.2"
"symfony/http-client": "^5.4 || ^6.4 || ^7.1",
"nyholm/psr7": "^1.8"
},
"scripts": {
"phpcs:check": "XDEBUG_MODE=off vendor/bin/php-cs-fixer check -v --diff",
"phpcs:fix": "XDEBUG_MODE=off vendor/bin/php-cs-fixer fix -v",
"phpstan": "XDEBUG_MODE=off php -d memory_limit=2048M vendor/bin/phpstan analyse",
"test": "XDEBUG_MODE=off vendor/bin/phpunit --testdox"
},
"config": {
"sort-packages": true
}
}
15 changes: 11 additions & 4 deletions examples/consume_website.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
<?php

/*
* Copyright (c) Fusonic GmbH. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*/

declare(strict_types=1);

use Symfony\Component\HttpClient\NativeHttpClient;
use Symfony\Component\HttpClient\Psr18Client;

require __DIR__ . "/../vendor/autoload.php";
require __DIR__.'/../vendor/autoload.php';

// Initialize new Psr\HttpClient implementation. This example uses Symfony's implementation from the symfony/http-client
// package but you can use any implementation provided by your framework of choice.
$client = new Psr18Client(new NativeHttpClient([ "headers" => [ "User-Agent" => "facebookexternalhit/1.1" ] ]));
// package, but you can use any implementation provided by your framework of choice.
$client = new Psr18Client(new NativeHttpClient(['headers' => ['User-Agent' => 'facebookexternalhit/1.1']]));

// Create a new crawler
$crawler = new Fusonic\OpenGraph\Consumer($client, $client);

// Crawl the desired URL and retrieve a Fusonic\OpenGraph\Object in response
$object = $crawler->loadUrl("https://github.com");
$object = $crawler->loadUrl('https://github.com');

var_dump($object);
Loading