如果我有这样的网址:
$url = graph.facebook.com/user-id/picture?type=large
我正在使用CURL
获取扩展程序。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$rawdata = trim(curl_exec($ch));
$results = split("\n", $rawdata);
var_dump($rawdata);
foreach($results as $line) {
if (strtok($line, ':') == 'Content-Type') {
$parts = explode(":", $line);
$extension = trim($parts[1]);
}
}
但是我无法获得图像的基本名称,任何人都可以帮助我吗?
答案 0 :(得分:2)
如果您只查看结果中图片的基本名称,为什么不搜索Location
而不是exploding
所有内容。
这样的事情:
foreach($results as $line) {
if( substr($line, 0, 10) == "Location: " ) {
$img_url= substr($line, 10);
echo basename($img_url,".jpg");
}
}
答案 1 :(得分:0)
使用vishwa提供的答案和我的想法我构建了这个
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$rawdata = trim(curl_exec($ch));
$results = split("\n", $rawdata);
//var_dump($rawdata);
echo "<br>";
$extension = '';
foreach($results as $line) {
if (strtok($line, ':') == 'Content-Type') {
$parts = explode(":", $line);
$extension = trim($parts[1]);
}
if($extension!='')
{
switch($extension){
case "image/gif": $extension='.gif';
$save=true;
break;
case "image/jpeg": $extension='.jpg';
$save=true;
break;
case "image/png": $extension='.png';
$save=true;
break;
default: $save=false;
}
if( substr($line, 0, 10) == "Location: " ) {
$img_url= substr($line, 10);
$base_name = explode($extension,basename($img_url,$extension));
echo $base_name = trim($base_name[0]);
}
}
}
谢谢