PHP code example of draliragab / socialize

1. Go to this page and download the library: Download draliragab/socialize library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

draliragab / socialize example snippets


$fb = Socialize::facebook()
        ->setMessage('Awesome message')
        ->setLink('https://github.com/')
        ->setAction(OgAction::FEELS, OgObject::EXCITED)
        ->sharePost();

dump($fb->getPostId()); // 123456789101112

$post = Post::first();
$response = $post->shareToFacebook();

echo $response->getPostId(); // 123456789101112

return [
    'facebook' => [
        'default' => [
            'app_id' => env('FACEBOOK_APP_ID'),
            'graph_version' => env('FACEBOOK_GRAPH_VERSION', 'v15.0'),
            'page_id' => env('FACEBOOK_PAGE_ID'),
            'page_access_token' => env('FACEBOOK_PAGE_ACCESS_TOKEN'),
        ],
    ],

    'instagram' => [
        'default' => [
            'graph_version' => env('INSTAGRAM_GRAPH_VERSION', 'v15.0'),
            'user_access_token' => env('INSTAGRAM_USER_ACCESS_TOKEN'),
            'instagram_account_id' => env('INSTAGRAM_ACCOUNT_ID'),
        ],
    ],

    'twitter' => [
        'default' => [
            'app_consumer_key' => env('TWITTER_CONSUMER_KEY'),
            'app_consumer_secret' => env('TWITTER_CONSUMER_SECRET'),
            'account_access_token' => env('TWITTER_ACCOUNT_ACCESS_TOKEN'),
            'account_access_token_secret' => env('TWITTER_ACCOUNT_ACCESS_TOKEN_SECRET'),
        ],
    ],

    'model_columns' => [
        'message_column' => 'title',
        'photo_column' => 'image',
    ],
];

return [
    'facebook' => [
        'default' => [
            'app_id' => env('FACEBOOK_APP_ID'),
            'graph_version' => env('FACEBOOK_GRAPH_VERSION', 'v15.0'),
            'page_id' => env('FACEBOOK_PAGE_ID'),
            'page_access_token' => env('FACEBOOK_PAGE_ACCESS_TOKEN'),
        ],

        'page_2' => [
            'app_id' => env('FACEBOOK_APP_ID_2'),
            'graph_version' => env('FACEBOOK_GRAPH_VERSION', 'v15.0'),
            'page_id' => env('FACEBOOK_PAGE_ID_2'),
            'page_access_token' => env('FACEBOOK_PAGE_ACCESS_TOKEN_2'),
        ],
    ],
];

$fb = Socialize::facebook('page_2');

$fb = Socialize::facebook()
        ->share(
            message: 'Awesome message',
            image: 'https:example.com/image.jpg',
            options: [],
        );

$postId = $fb->getPostId();

$fb = Socialize::facebook();

use DrAliRagab\Socialize\Socialize;

$fb = Socialize::facebook('page_2');

setBackdatedTime(float|Carbon $backdated_time)
setBackdatedTimeGranularity(string $backdated_time_granularity) // One of ['year', 'month', 'day', 'hour', 'minute']
addChildAttachment(string $link, string $name = null, string $description = null, string $image_hash = null, string $picture = null)
setFeedTargeting(int $age_max = null, int $age_min = null, array $college_years = null, array $education_statuses = null, array $genders = null, array $geo_locations = null, array $interests = null, int $locales = null, array $relationship_statuses = null)
setLink(string $link)
setMessage(string $text)
setMultiShareEndCard(bool $multi_share_end_card = true)
setMultiShareOptimized(bool $multi_share_optimized = true)
setObjectAttachment(string $object_attachment)
setPlace(string $placeId)
setPublished(bool $published = true)
setScheduledPublishTime(int|Carbon $scheduled_publish_time)
setTags(array $tagsIds)
setTargeting(int $age_min = null, array $geo_locations = null)
setAction(OgAction $action, OgObject $object, ?OgIcon $icon = null)
attachMedia(int $mediaId)
attachMedias(array $mediaIds)

// After setting the options you can share the post
sharePost()

// Also after sharing you can add comments to the post
addComment('Awesome comment')

// And you ca delete the post
deletePost(int $postId) // returns true if the post is deleted successfully

use DrAliRagab\Socialize\Socialize;
use DrAliRagab\Socialize\Enums\OpenGraph\OgAction;
use DrAliRagab\Socialize\Enums\OpenGraph\OgObject;

$fb = Socialize::facebook();
$response = $fb
    ->setBackdatedTime(now()->subDays(2))
    ->setBackdatedTimeGranularity('day')
    ->addChildAttachment(
        link: 'https://example.com/1',
        name: "Awesome name",
        description: "Awesome description",
        picture: "https://example.com/image.jpg"
    )
    ->addChildAttachment(
        link: 'https://example.com/2',
        name: "Awesome name 2",
        description: "Awesome description 2",
        picture: "https://example.com/image2.jpg"
    )
    ->setFeedTargeting(
        age_max: 65,
        age_min: 18,
    )
    ->setLink('https://example.com')
    ->setMessage('Awesome message')
    ->setMultiShareEndCard(true)
    ->setMultiShareOptimized(true)
    ->setPublished(true)
    ->setTargeting(
        age_min: 18
    )
    ->setAction(OgAction::FEELS, OgObject::EXCITED)
    ->sharePost()   // Must be called after setting the options
    ->addComment('Awesome comment');    // Must be called after sharing the post

$postId = $response->getPostId();   // Get the post id

// Delete the post
$deleted = $fb->deletePost($postId);

uploadPhoto(string $photoUrl, string $caption = null, bool $published = false, bool $temporary = true)

$fb = Socialize::facebook();
$mediaId = $fb->uploadPhoto('https://example.com/image.jpg')->getMediaId();

// You can use the media id to attach it to a post
$postId = Socialize::facebook()
    ->attachMedia($mediaId)
    ->setMessage('Awesome image')
    ->sharePost()
    ->getPostId();

$photoId = Socialize::facebook()
    ->uploadPhoto('https://example.com/image.jpg', 'Awesome image', true, false)
    ->getMediaId(); // returns the photo id

$deleted = Socialize::facebook()->deletePhoto($photoId); // returns true if the photo is deleted successfully

$data = Socialize::facebook()-getPosts();

$twitter = Socialize::twitter();

use DrAliRagab\Socialize\Socialize;

$twitter = Socialize::twitter('account_2');

$twitter = Socialize::twitter();
$postId = $twitter->tweet('Awesome tweet')->getPostId();

superFollowersOnly()
addPlace(string $placeId)
addPoll(array $pollOptions, int $pollDuration)
quoteTweet(string $tweetId)
restrictReply(string $restrictReply)    // "mentionedUsers" and "following" are the only options
inReplyTo(string $tweetId)
addMedia(array $mediaIds)
tagUsers(array $usernames)

$postId = $twitter
    ->superFollowersOnly()
    ->addPoll(
        pollOptions: ['Disappointed 😞', 'Predictable 😐', 'Excited 😃'],
        pollDuration: 60,
    )
    ->quoteTweet('12345679101112')
    ->restrictReply('mentionedUsers')
    ->inReplyTo('12345679101112')
    ->tweet(
        text: 'https://example.com/',
    )->getPostId();


$postId = $twitter
    ->tweet('Awesome tweet')
    ->addComment('Awesome comment')
    ->getPostId();

$imgPath = public_path('default-page-img.png');
$imgPath2 = public_path('default-page-img2.png');

$mediaIds = $twitter->uploadMedia([
    $imgPath,
    $imgPath2,
])->getMediaIds();

$postId = $twitter
    ->addMedia($mediaIds)
    ->tweet('Awesome tweet')->getPostId();

$postId = $twitter
    ->uploadMedia([$imgPath])
    ->addMedia()
    ->tweet('Awesome tweet')
    ->getPostId();

$insta = Socialize::instagram();

use DrAliRagab\Socialize\Socialize;

$insta = Socialize::instagram('account_2');

$insta = Socialize::instagram();
$postId = $insta->publishImage('https://example.com/image.jpg', 'Awesome image')->getPostId();

$postUrl = $insta
    ->publishImageCarousel([
        'https://example.com/image.jpg',
        'https://example.com/image2.jpg',
        'https://example.com/image3.jpg',
    ], 'Awesome image')
    ->addComment('Awesome image')
    ->getUrl();

$postId = Socialize::instagram()
    ->publishImage('https://example.com/image.jpg', 'Awesome image')
    ->addComment('Awesome image', $postId)
    ->getPostId();

$postUrl = Socialize::instagram()
    ->publishImage('https://example.com/image.jpg', 'Awesome image')
    ->getUrl();

use DrAliRagab\Socialize\Traits\Socializer;

class Post extends Model
{
    use Socializer;
}

$post = Post::find(1);

$post->shareToFacebook();   // share to facebook
$post->shareToTwitter();    // share to twitter
$post->shareToInstagram();  // share to instagram

'model_columns' => [
    'message_column' => 'title',
    'photo_column' => 'image',
],

$post->shareToInstagram([
    'photo' => 'https://example.com/image.jpg',
    'message' => 'Awesome post',
    'options' => [
        'scheduled_publish_time' => now()->addDays(2)->timestamp,
    ],
]);

$post->shareToTwitter([
    'photo' => public_path('default-page-img.png'),
    'message' => 'Awesome post',
    'config' => 'account_2',
]);
bash
php artisan vendor:publish --tag="socialize-config"