0

I have this function:

function Connect($url, $post = 0, $postfields = '') 
{
    $ch = curl_init();
    if($post > 0) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
    } 
    curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/joomla-cookie.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__).'/joomla-cookie.txt');
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $exec = curl_exec($ch);
    if($exec) { return $exec; } else { return 0; }
}

I call it like that Connect($host)

And it always return 0...

hakre
  • 193,403
  • 52
  • 435
  • 836
Danpe
  • 18,668
  • 21
  • 96
  • 131
  • `$host` suggests you are sending "google.com" , where as the url needed is `http://google.com`, what are you using? – Sabeen Malik Jun 08 '11 at 22:57
  • @Sabeen i acctually sending it with the https:// Maybe it's cuz https:// and not http:// ? cuz "http://www.google.com" works. – Danpe Jun 08 '11 at 23:05

2 Answers2

2

Try setting CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST to false. that should do the trick.

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
Sabeen Malik
  • 10,816
  • 4
  • 33
  • 50
  • @Sabeen There is a way to enable the cookie to support SSL too? Cuz the session only saves when im using http:// ... – Danpe Jun 08 '11 at 23:27
  • I don't think the https should effect the way cookies are stored. Look at the joomla-cookie.txt file to see what it contains, that might give u some clues. – Sabeen Malik Jun 08 '11 at 23:38
  • @Sabeen Yup i dont think it should effect it also, but it does lol. I have no clue in cookies.. Here is the pastebin: http://pastebin.com/RX9vnEbT – Danpe Jun 08 '11 at 23:41
  • 1
    Are you moving between http and https? – Sabeen Malik Jun 08 '11 at 23:41
  • @Sabeen I think so, not sure though. – Danpe Jun 08 '11 at 23:42
  • 1
    try to make sure that you start with https and submit to https. I believe if you will cross over between https and http, you will loose the cookie. Also is the domain the same for https as well. sometimes sites have https://secure.domain.com etc – Sabeen Malik Jun 08 '11 at 23:44
  • Please note that [disabling VERIFYPEER or VERIFYHOST makes the connection vulnerable to MITM attacks](http://stackoverflow.com/a/13742121/372643). – Bruno Nov 21 '14 at 11:27
1

Check if there was an error with the request after curl_exec:

if(curl_errno($ch)){
    echo 'Curl error: ' . curl_error($ch);
}

That will provide you with enough info to know if there was a error with the request. If there was no error, you can check the request sent after curl_exec so you can double check that everything sent is in place:

print_r(curl_getinfo($ch));
rzetterberg
  • 10,146
  • 4
  • 44
  • 54
  • Thank You ! i get SSL error: Curl error: SSL certificate problem, verify that the CA cert is OK. Detai ls: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify faile dSession ID: Curl error: SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed So it must be because of the https://.. – Danpe Jun 08 '11 at 23:12