【发布时间】:2010-04-09 20:33:13
【问题描述】:
我使用过mime_content_type() 和文件信息,但我从未成功过。我现在想将 cURL 与 PHP 一起使用,并获取托管在另一个域上的文件的标题,然后提取并确定类型是否为 MP3。 (我认为 MP3 的 mime 类型是audio/mpeg)
简而言之,我知道但我不知道如何应用它:)
谢谢
【问题讨论】:
标签: php curl mime-types
我使用过mime_content_type() 和文件信息,但我从未成功过。我现在想将 cURL 与 PHP 一起使用,并获取托管在另一个域上的文件的标题,然后提取并确定类型是否为 MP3。 (我认为 MP3 的 mime 类型是audio/mpeg)
简而言之,我知道但我不知道如何应用它:)
谢谢
【问题讨论】:
标签: php curl mime-types
<?php
# the request
$ch = curl_init('http://www.google.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
# get the content type
echo curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
# output
text/html; charset=ISO-8859-1
?>
curl -I http://www.google.com
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Fri, 09 Apr 2010 20:35:12 GMT
Expires: Sun, 09 May 2010 20:35:12 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
【讨论】:
您可以通过 curl 使用 HEAD 请求。喜欢:
$ch = curl_init();
$url = 'http://sstatic.net/so/img/logo.png';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$results = explode("\n", trim(curl_exec($ch)));
foreach($results as $line) {
if (strtolower(strtok($line, ':')) == 'content-type') {
$parts = explode(":", $line);
echo trim($parts[1]);
}
}
返回:image/png
【讨论】:
如果您可以使用更优雅的 Zend Framework 版本,here is a class 它使用 Zend_Http_Client 组件。
像这样使用它:
$sniffer = new Smartycode_Http_Mime();
$contentType = $sniffer->getMime($url);
【讨论】: