0

Hoping someone can point me in the right direction. I have a URL that redirects to a media file. That is the information I am trying to retrieve. (The host and the path of the actual file).

Using this code

<?php 
$url='http://urs.pbs.org/redirect/71201be9214242cbbc32633826f8092b';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_exec($ch);
$newurl=curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$p = parse_url($newurl) ;
$rtmp=$p['scheme']."://".$p['host'];
$episode=$p['path'];
echo $rtmp.'<BR>';
echo $episode.'<BR>';
?>

Locally, it works fine. But on my production server is just kicks back the $url.

It prints out http://urs.pbs.org /redirect/71201be9214242cbbc3233826f8092b

Also, I should point out that CURL is installed on the production server and working fine. I am not getting any errors.

Any help appreciated.

  • Can you turn on verbose debugging? CURLOPT_DEBUG to true and you need to set CURLOPT_DEBUGFUNCTION. Gives us that output or let us know that output solved it. – Motomotes Mar 15 '13 at 22:18
  • @motes Sorry, could you send me an example. I can't seem to find anything about CURLOPT_DEBUG at [http://php.net/manual/en/function.curl-setopt.php] (the php CURL page) – Creative Links Mar 16 '13 at 03:26
  • My fault, its [CURLOPT_VERBOSE](http://stackoverflow.com/questions/3757071/php-debugging-curl) set to true, and you dont **need** to set the CURLOPT_DEBUGFUNCTION to a callback that will recieve the debug info, by default it will be sent to STDERR, our you can set CURLOPT_STDERR to a open file handle and the debug info will be saved there. I use cURL in C and always use a callback so I can see whats happening in libcURL in realtime by outputing to the debug window. – Motomotes Mar 16 '13 at 05:46

1 Answers1

1

CURLOPT_FOLLOWLOCATION cannot be set true if safe mode is enabled, which is the case for many production environments. In this case tho a warning is triggered, something like this:

Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in /home/xxx/public_html/xxx.php on line 56

My solution for this situation is manually following redirects for say 10 levels, making recursive curl requests.

Update:

Also CURLOPT_RETURNTRANSFER tells curl to return the result like this: $result = curl_exec($ch); so if you want it to print, put an echo before curl_exec()

aorcsik
  • 15,271
  • 5
  • 39
  • 49
  • Why wouldn't it being printing header, if it was onlt Location header causing the problem? – Motomotes Mar 15 '13 at 22:26
  • 1
    `CURLOPT_RETURNTRANSFER` tells curl to return the result like this: `$result = curl_exec($ch);` so if you want it to print, put an `echo` before `curl_exec()` – aorcsik Mar 15 '13 at 22:30
  • @aorcsik Thanks for the tip. I realized I have warnings turned off -- so sure enough that's the issue. Thanks! – Creative Links Mar 16 '13 at 03:59