縮短您的網址
API 使用說明
選擇您偏好的程式語言來查看使用範例:
<?php
//ini_set("display_errors",1);
header("Content-type: text/plain; charset=utf-8");
// Target URL for redirection service
$apiEndpoint = "https://url.care";
// URL to be shortened
$url = "https://www.example.com"; // Replace with your desired URL
// Prepare cURL request
$ch = curl_init($apiEndpoint);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode(['url' => $url]),
CURLOPT_HTTPHEADER => [
'Content-Type: application/json'
]
]);
// Execute cURL request
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// Process response
if ($httpCode === 200) {
$result = json_decode($response, true);
echo "Shortened URL: " . $result['url'] . "\n";
echo "Alternative URL: " . $result['alt_url'] . "\n";
} else {
echo "Error: " . $response . "\n";
}
// 使用 API 金鑰的版本
/*
<?php
header('Content-Type: text/plain; charset=utf-8');
// API 端點
$api_endpoint = 'https://url.care/api/shorten';
$api_key = 'YOUR_API_KEY'; // 替換為您的 API 金鑰
// 要縮短的網址
$url = 'https://www.example.com'; // 替換為您想要縮短的網址
// 準備 cURL 請求
$ch = curl_init($api_endpoint);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode(['url' => $url]),
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'X-API-Key: ' . $api_key
]
]);
// 執行請求
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// 處理響應
if ($httpCode === 200) {
$result = json_decode($response, true);
echo "短網址: " . $result['url'] . "\n";
echo "替代網址: " . $result['alt_url'] . "\n";
} else {
echo "錯誤: " . $response . "\n";
}
*/
import requests
import json
# API 端點
api_endpoint = "https://url.care"
# 要縮短的網址
url = "https://www.example.com" # 替換為您想要縮短的網址
# 準備請求數據
data = {
"url": url
}
# 發送 POST 請求
try:
response = requests.post(api_endpoint, json=data)
response.raise_for_status() # 檢查是否有錯誤
# 解析響應
result = response.json()
print(f"短網址: {result['url']}")
print(f"替代網址: {result['alt_url']}")
except requests.exceptions.RequestException as e:
print(f"錯誤: {e}")
# 使用 API 金鑰的版本
"""
import requests
import json
# API 端點
api_endpoint = "https://url.care/api/shorten"
api_key = "YOUR_API_KEY" # 替換為您的 API 金鑰
# 要縮短的網址
url = "https://www.example.com" # 替換為您想要縮短的網址
# 準備請求數據
data = {
"url": url
}
# 準備請求頭
headers = {
"X-API-Key": api_key,
"Content-Type": "application/json"
}
# 發送 POST 請求
try:
response = requests.post(api_endpoint, json=data, headers=headers)
response.raise_for_status() # 檢查是否有錯誤
# 解析響應
result = response.json()
print(f"短網址: {result['url']}")
print(f"替代網址: {result['alt_url']}")
except requests.exceptions.RequestException as e:
print(f"錯誤: {e}")
"""
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
// API 端點
string apiEndpoint = "https://url.care";
// 要縮短的網址
string url = "https://www.example.com"; // 替換為您想要縮短的網址
// 準備請求數據
var data = new { url = url };
string jsonData = JsonSerializer.Serialize(data);
using (var client = new HttpClient())
{
try
{
// 發送 POST 請求
var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
var response = await client.PostAsync(apiEndpoint, content);
response.EnsureSuccessStatusCode();
// 解析響應
string responseBody = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<UrlResponse>(responseBody);
Console.WriteLine($"短網址: {result.url}");
Console.WriteLine($"替代網址: {result.alt_url}");
}
catch (Exception ex)
{
Console.WriteLine($"錯誤: {ex.Message}");
}
}
}
}
// 響應數據類
public class UrlResponse
{
public string url { get; set; }
public string alt_url { get; set; }
}
// 使用 API 金鑰的版本
/*
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
// API 端點
string apiEndpoint = "https://url.care/api/shorten";
string apiKey = "YOUR_API_KEY"; // 替換為您的 API 金鑰
// 要縮短的網址
string url = "https://www.example.com"; // 替換為您想要縮短的網址
// 準備請求數據
var data = new { url = url };
string jsonData = JsonSerializer.Serialize(data);
using (var client = new HttpClient())
{
try
{
// 設置請求頭
client.DefaultRequestHeaders.Add("X-API-Key", apiKey);
// 發送 POST 請求
var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
var response = await client.PostAsync(apiEndpoint, content);
response.EnsureSuccessStatusCode();
// 解析響應
string responseBody = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<UrlResponse>(responseBody);
Console.WriteLine($"短網址: {result.url}");
Console.WriteLine($"替代網址: {result.alt_url}");
}
catch (Exception ex)
{
Console.WriteLine($"錯誤: {ex.Message}");
}
}
}
}
// 響應數據類
public class UrlResponse
{
public string url { get; set; }
public string alt_url { get; set; }
}
*/