When pulling a URL from a MYSQL database, my PHP cURL function will not work

I use this function for my cURL requests.

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 fine, but when I try to run it using the URL from my MySQL database, it will not work ...

I did the following tests ... I'm trying to get the source of the video on YouTube:

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 that my request 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'm not sure what to do or something is wrong. Any suggestions?

+3
source share
2 answers

Try setting CURLOPT_FOLLOWLOCATIONto trueso that the page can be redirected if necessary.

+2
source

CURLOPT_FOLLOWLOCATION false? URL- -www youtube www, , , , . , followlocation. fopen , (file_get_contents($video)).

+2

All Articles