Dưới đây là các ví dụ về cách gọi API của chúng tôi bằng các ngôn ngữ lập trình phổ biến. Hãy thay thế YOUR_API_KEY
bằng khóa API thực tế của bạn.
<?php
$apiKey = 'YOUR_API_KEY';
$facebookLink = 'https://www.facebook.com/ToolvnOfficial/';
$apiUrl = 'https://tool.vn/api/facebook/get-id-from-link';
$postData = json_encode(['link' => $facebookLink]);
$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $apiKey,
'Content-Length: ' . strlen($postData)
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "HTTP Status Code: " . $httpCode . "\n";
echo "Response Body:\n";
print_r(json_decode($response, true));
?>
Trước tiên, hãy cài đặt thư viện axios: npm install axios
const axios = require('axios');
const apiKey = 'YOUR_API_KEY';
const facebookLink = 'https://www.facebook.com/ToolvnOfficial/';
const apiUrl = 'https://tool.vn/api/facebook/get-id-from-link';
const data = {
link: facebookLink
};
const config = {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
};
axios.post(apiUrl, data, config)
.then(response => {
console.log('Status:', response.status);
console.log('Data:', response.data);
})
.catch(error => {
if (error.response) {
// Yêu cầu đã được thực hiện và máy chủ đã phản hồi với mã trạng thái khác 2xx
console.error('Error Status:', error.response.status);
console.error('Error Data:', error.response.data);
} else if (error.request) {
// Yêu cầu đã được thực hiện nhưng không nhận được phản hồi
console.error('No response received:', error.request);
} else {
// Lỗi xảy ra khi thiết lập yêu cầu
console.error('Error:', error.message);
}
});
Trước tiên, hãy cài đặt thư viện requests: pip install requests
import requests
import json
api_key = "YOUR_API_KEY"
facebook_link = "https://www.facebook.com/ToolvnOfficial/"
api_url = "https://tool.vn/api/facebook/get-id-from-link"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = {
"link": facebook_link
}
try:
response = requests.post(api_url, headers=headers, json=data)
response.raise_for_status() # Ném ra lỗi nếu mã trạng thái là 4xx hoặc 5xx
print(f"Status Code: {response.status_code}")
print("Response JSON:")
print(json.dumps(response.json(), indent=4))
except requests.exceptions.HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
print(f"Response content: {response.text}")
except Exception as err:
print(f"Other error occurred: {err}")