HTTP 302 応答を処理し、特定の URL の実際のリンクを返すスクリプトが必要です。たとえば、http://185.20.36.231:8080/stream/?channel=nettvinfo&stream=1mb&sp=pass1@&b=3&u=user1
それは返す必要がありますhttp://185.20.36.233:8080/stream/?fw=true&route=at1_1&rule=null&sp=nettvplus&channel=nettvinfoint&stream=1mb&u=user1&b=3&player=http
次のスクリプトを試しましたが、何らかの理由でこの特定の URL では機能しません。助けてください。
<?php
function getOriginalURL($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// if it's not a redirection (3XX), move along
if ($httpStatus < 300 || $httpStatus >= 400)
return $url;
// look for a location: header to find the target URL
if(preg_match('/location: (.*)/i', $result, $r)) {
$location = trim($r[1]);
// if the location is a relative URL, attempt to make it absolute
if (preg_match('/^\/(.*)/', $location)) {
$urlParts = parse_url($url);
if ($urlParts['scheme'])
$baseURL = $urlParts['scheme'].'://';
if ($urlParts['host'])
$baseURL .= $urlParts['host'];
if ($urlParts['port'])
$baseURL .= ':'.$urlParts['port'];
return $baseURL.$location;
}
return $location;
}
return $url;
}
$newurl=getOriginalURL("http://185.20.36.231:8080/stream/?channel=nettvinfo&stream=1mb&sp=pass1@&b=3&u=user1");
print($newurl);
?>
編集>
@farmer1992
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, fopen('log2.txt', 'a+'));
ログファイル
* About to connect() to 185.20.36.231 port 8080 (#4)
* Trying 185.20.36.231...
* Adding handle: conn: 0x29a98d0
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 4 (0x29a98d0) send_pipe: 1, recv_pipe: 0
* Connection timed out
* Failed connect to 185.20.36.231:8080; Connection timed out
* Closing connection 4