phpweby
phpweby

Reputation: 43

Curl request not working on live server

Can anybody tell me why this CURL code only works on my local server and not on live server? Tried on 3 different hosting and nothing works.

Checked everything on live 1) Curl enabled 2) PHP version is OK 3) Curl executes without any error but no result

Its been 3 days and I am not able to find any solution please help.

error_reporting(1);
set_time_limit(1500);

$fname=time().'_myfile.flv';
header('Content-type: video/x-flv');
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$fname\"");

define('USERAGENT', "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.2; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)");
$url='http://v3.lscache5.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor&fexp=914010%2C907605&algorithm=throttle-factor&itag=34&ip=112.0.0.0&burst=40&sver=3&signature=D51A660BDF83B54B3584425DBE8930D5D0F805E1.B3FB21D0CAF625D36A17B558A0A653F20788B49F&expire=1313503200&key=yt1&ipbits=8&factor=1.25&id=1cacd26a9913e4ec';


    $ch = curl_init() or die("Error");
    curl_setopt($ch, CURLOPT_USERAGENT, USERAGENT);
    curl_setopt($ch, CURLOPT_URL, $url);    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);  
    if(curl_exec($ch) === FALSE) 
    {
        die("Curl failed: " . curl_error($ch));  // Never goes here
    }

    curl_close($ch);

?>

Upvotes: 4

Views: 28565

Answers (5)

Adrian Hojny
Adrian Hojny

Reputation: 1

I'm sure it'll help you (I've been working through this problem alone for a few days)

add an agent to your cURL:

(option when we have crul_opt in the array)

 CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.9) Gecko/20100508 SeaMonkey/2.0.4',

(option when setting curl individually)

curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1');

I noticed that it works because postman gave me the code that I pasted mindlessly into the files, although it did not include an agent (for localhost and in postman it works) and live hosting did not support it. (the postman himself adds his agent)

Upvotes: 0

Jeff Solomon
Jeff Solomon

Reputation: 473

I had this issue for several days, was not able to find any errors in the PHP and the cURL response seemed to come back completely null. Finally found suggested code to put into the cURL request

if (curl_exec($curl) === FALSE) {
   die("Curl Failed: " . curl_error($curl));
} else {
   return curl_exec($curl);
}

Adding this finally gave me an error in the PHP which was:

Curl Failed: SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

Searching that error on SO gave me this: HTTPS and SSL3_GET_SERVER_CERTIFICATE:certificate verify failed, CA is OK

Which lead me to add code to my cURL request that essentially disables the SSL verification.

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

I'm not working w/ my server admin to find a better solution because I don't know if this workaround is so good, but for now this works.

Upvotes: 7

Álvaro González
Álvaro González

Reputation: 146588

Some things to check:

  1. Your value for error_reporting() is pretty weird: 1 equals E_ERROR and implies that you are ignoring almost everything, including warnings. I would not recommend that even in production, not to mention development.

  2. You define $header_list and never use it.

  3. Make sure you are inspecting the real ouput, not the output as rendered by a browser.

Upvotes: 0

Karoly Horvath
Karoly Horvath

Reputation: 96286

Test it with

error_reporting(E_ALL);

Upvotes: 1

Arnaud Le Blanc
Arnaud Le Blanc

Reputation: 99921

Do a curl_getinfo($ch) after exec to see the response code returned by the server.

Upvotes: 4

Related Questions