bluesky-Connector/includes/bluesky-api.php

70 lines
2.1 KiB
PHP
Raw Normal View History

2024-11-21 09:05:37 +00:00
<?php
class Bluesky_API {
private $api_url = 'https://bsky.social/xrpc'; // Replace with the actual API endpoint
private $api_key;
private $did;
public function __construct($api_key, $did) {
$this->api_key = $api_key;
$this->did = $did;
}
public function create_post($post_data) {
$headers = array(
'Authorization' => 'Bearer ' . $this->api_key,
'Content-Type' => 'application/json',
);
$response = wp_remote_post($this->api_url . '/com.atproto.repo.createRecord', array(
'headers' => $headers,
'body' => json_encode(array(
'repo' => $this->did,
'collection' => 'app.bsky.feed.post',
'record' => $post_data,
)),
));
if (is_wp_error($response)) {
return array('error' => $response->get_error_message());
}
return json_decode(wp_remote_retrieve_body($response), true);
}
public function resolve_handle($handle) {
$response = wp_remote_get($this->api_url . '/com.atproto.identity.resolveHandle', array(
'query' => array(
'handle' => $handle,
),
));
if (is_wp_error($response)) {
return array('error' => $response->get_error_message());
}
return json_decode(wp_remote_retrieve_body($response), true);
}
public function upload_blob($file_path, $mime_type) {
$headers = array(
'Authorization' => 'Bearer ' . $this->api_key,
'Content-Type' => $mime_type,
);
$file_contents = file_get_contents($file_path);
if ($file_contents === false) {
return array('error' => 'Failed to read file.');
}
$response = wp_remote_post($this->api_url . '/com.atproto.repo.uploadBlob', array(
'headers' => $headers,
'body' => $file_contents,
));
if (is_wp_error($response)) {
return array('error' => $response->get_error_message());
}
return json_decode(wp_remote_retrieve_body($response), true);
}
}