Reputation: 4490
I am using this function for my cURL queries.
function get_source($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt ($ch, CURLOPT_HEADER, 0);
$source = curl_exec ($ch);
return $source;
}
The function works perfectly, but when I try to run it on a URL from my MySQL database, it wont work...
I have done the following tests...I am trying to get the source of a YouTube video:
Test 1:
echo get_source("http://www.youtube.com/watch?v=WxfZkMm3wcg");
**Result: Works, returns the source code of the video.**
Test 2:
$video="http://www.youtube.com/watch?v=WxfZkMm3wcg";
echo get_source($video);
**Result: Works, returns the source code of the video.**
Test 3:
$video_arr=mysql_fetch_array(mysql_query("SELECT video FROM videos WHERE id='$video_id'"));
$video=$video_arr['video'];
echo get_source($video);
**Result: Does not work. A blank string gets returned and there aren't any cURL errors that I can see...**
Test 4 (just to show my query is working)
$video_arr=mysql_fetch_array(mysql_query("SELECT video FROM videos WHERE id='$video_id'"));
$video=$video_arr['video'];
var_dump($video);
**Result: string(38) "http://youtube.com/watch?v=WxfZkMm3wcg"**
I am not sure what to do or what is even going wrong. ANy suggestions?
Upvotes: 3
Views: 734
Reputation: 4211
Try setting CURLOPT_FOLLOWLOCATION
to true
so the page can redirect if necessary.
Upvotes: 2
Reputation: 191779
Why do you set CURLOPT_FOLLOWLOCATION
to false
? The non-www url for youtube is trying to redirect to www, but without followlocation, all you get back is a header requesting to redirect. You either need to follow that manually or turn followlocation
on. You may want to use the fopen wrappers instead for simplicity if they are available (file_get_contents($video)
).
Upvotes: 2