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?
Try setting CURLOPT_FOLLOWLOCATION to true so the page can redirect if necessary.
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)).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With