PHP 代码示例
本页面提供 PHP 调用哼哼猫 API 的完整示例。
🚀 使用 file_get_contents
<?php
$apiUrl = "https://api.meowload.net/openapi/extract/post";
$apiKey = "your-api-key-here";
$params = array("url" => "https://www.youtube.com/watch?v=dQw4w9WgXcQ");
$options = array(
"http" => array(
"header" =>
"Content-Type: application/json\r\n" .
"x-api-key: " . $apiKey . "\r\n" .
"accept-language: zh",
"method" => "POST",
"content" => json_encode($params),
),
"ssl" => array(
"verify_peer" => false,
"verify_peer_name" => false,
),
);
$context = stream_context_create($options);
$response = file_get_contents($apiUrl, false, $context);
// 解析 HTTP 响应状态码
$statusLine = $http_response_header[0];
preg_match('{HTTP\/\S*\s(\d{3})}', $statusLine, $match);
$statusCode = $match[1];
$data = json_decode($response, true);
if ($statusCode == 200) {
echo "✅ 提取成功!\n";
echo "媒体数量: " . count($data['medias']) . "\n";
foreach ($data['medias'] as $media) {
echo "- " . $media['media_type'] . ": " . $media['resource_url'] . "\n";
}
} else {
echo "❌ 请求失败 (" . $statusCode . "): " . $data['message'] . "\n";
}
?>📦 使用 cURL
<?php
function extractPost($url, $apiKey) {
$apiUrl = "https://api.meowload.net/openapi/extract/post";
$ch = curl_init($apiUrl);
$payload = json_encode(array("url" => $url));
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'x-api-key: ' . $apiKey,
'accept-language: zh'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$data = json_decode($response, true);
if ($statusCode == 200) {
return $data;
} else {
throw new Exception($data['message']);
}
}
// 使用
try {
$result = extractPost("https://www.youtube.com/watch?v=dQw4w9WgXcQ", "your-api-key-here");
echo "✅ 提取成功!媒体数量: " . count($result['medias']) . "\n";
} catch (Exception $e) {
echo "❌ 错误: " . $e->getMessage() . "\n";
}
?>