0

I'm trying to call an url in curl to launch a backup, which is working perfectly in my browser. Curl return 0 so there seems to be no error, however it doesn't do the backup.

Here is my code:

public function runBackup(){

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, 'http://localhost/bnc_update/kybackup/backup-in-background');
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Requested-With: XMLHTTPRequest','User-Agent: KaliMessenger'));

    curl_exec($ch);

    if( ! $result = curl_exec($ch)) 
    { 
        trigger_error(curl_error($ch)); 
        echo '<pre>';
        print_r(curl_getinfo($ch));
        echo '</pre>';
        echo 'Erreur Curl : ' . curl_errno($ch) . '<br/>';

    }  
    echo 'Result: <br/>';
    var_dump($result);

    curl_close($ch);
    exit();
}

Which output:

Array
(
[url] => http://localhost/bnc_update/kybackup/backup-in-background
[content_type] => text/html; charset=UTF-8
[http_code] => 301
[header_size] => 656
[request_size] => 149
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.162641
[namelookup_time] => 6.9E-5
[connect_time] => 0.000186
[pretransfer_time] => 0.000214
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => 0
[upload_content_length] => 0
[starttransfer_time] => 0.162623
[redirect_time] => 0
[redirect_url] => http://localhost/bnc_update/sign-in?back-uri=kybackup%252Fbackup-in-background
[primary_ip] => ::1
[certinfo] => Array
    (
    )

[primary_port] => 80
[local_ip] => ::1
[local_port] => 56883
)

Erreur Curl : 0
Result:
string(0) ""

I see there is a redirect_url, could it be the problem? I'm already logged in in the browser though, do I need to perform another request to login again?

Kalianey
  • 2,738
  • 6
  • 25
  • 43
  • 1
    That's because no error occurred. You got a 301 redirect, which is **NOT** an error. since you didn't tell curl it was allowed to follow the redirect, it stops right there with a non-failure (e.g. success). – Marc B Jun 09 '15 at 19:10
  • How can I follow the redirect? – Kalianey Jun 09 '15 at 20:00
  • http://stackoverflow.com/questions/3519939/make-curl-follow-redirects – Marc B Jun 09 '15 at 20:04
  • Thanks :), so now it follow the redirect but still doesn't perform the backup, it just return `Result: string(2) "[]" ` – Kalianey Jun 09 '15 at 20:34
  • which is probably a json response, consisting of an empty array. – Marc B Jun 09 '15 at 20:34
  • Yes because it can't login with no credentials, but I don't understand why I need to login in the first place since I already have a session, any way to go around this? Or can I pass the login credential in the redirect somehow? – Kalianey Jun 09 '15 at 20:41
  • you didn't provide any cookies to curl, and started a brand new fresh curl, so I can't see how you could possibly have an established session in that particular curl instance... – Marc B Jun 09 '15 at 20:42
  • hum, and how can I provide this cookie to curl? – Kalianey Jun 09 '15 at 20:48
  • 1
    curlopt_cookiejar and curlopt_cookiefile. – Marc B Jun 09 '15 at 21:14

0 Answers0