-1

I can't save cookiefile when I use curl login with json in any hosting. Here is my code:

<?php
function curl($url = false, $var = false, $cookie = false, $header = false)
{
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
        if($var){
  curl_setopt($ch, CURLOPT_POSTFIELDS,$var);
        }
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
  curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); 
  curl_setopt($ch, CURLOPT_REFERER, $header);
  @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
   'Content-Type: application/json',
            'charset=utf-8',
   'Content-Length: ' . strlen($var)
        )); 
  $error = curl_error($ch);
  $result = curl_exec($ch);
  curl_close ($ch);
  return $result;
        return $error;
}
    $urlcheck = 'http://abcxyz.com/api/login';
    $username = 'xxxxxxxx';
    $password = 'xxx';
    $passmd5 = @md5($password);
 $request = '{"username":"'.$username.'","password":"'.$password.'","passwordMD5":"'.$passmd5.'"}';
 $data = json_decode($request);
 if (empty($data->username) || empty($data->password)){
  echo " Thieu du lieu";
 }else{
  $ck = str_replace('\\','/',dirname(__FILE__)).'/cookies/'.$data->username.'.txt'; 
        $curl = curl($urlcheck,$request,$ck);
        echo $curl; 
  }
?>
I've chmod 0777 directory /cookies/.
Navin
  • 3,681
  • 3
  • 28
  • 52
Red Hat
  • 3
  • 1

3 Answers3

0

If your request is supposed to be a POST request you had not instructed curl to send a POST request. The below is slightly different to your function in that there is no $cookie parameter to the function - the curl request will use the system temp directory to store the cookie.

<?php
    function curl( $url = false, $var = false, $header = false ){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);

        if( $var ){
            /* send POST request */
            curl_setopt($ch, CURLOPT_POST,true);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$var);
        }
        /* Use the systems temp dir to store cookies */
        $cookiejar=tempnam( sys_get_temp_dir(), 'cookiejar_' );
        $headers=array('Content-Type: application/json','charset=utf-8');
        if( $var ) $headers[]='Content-Length: ' . strlen($var);

        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); 
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar );
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiejar ); 
        curl_setopt($ch, CURLOPT_REFERER, $header );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);     
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers ); 

        $error = curl_error($ch);
        $result = curl_exec($ch);
        curl_close($ch);
        /* you can't expect two return statements to both return data ! */
        return $result;
    }

    $urlcheck = 'http://abcxyz.com/api/login';
    $username = 'xxxxxxxx';
    $password = 'xxx';
    $passmd5 = md5($password);
    $request = '{"username":"'.$username.'","password":"'.$password.'","passwordMD5":"'.$passmd5.'"}';
    $data = json_decode($request);

    if ( empty($data->username) || empty($data->password) ){
        echo " Thieu du lieu";
    }else{
        $curl = curl( $urlcheck, $request );
        echo $curl; 
    }
?>      
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • My problem is not saved cookies. I want to save a cookie file to curl again. I tested your code it worked but not saved cookie file. This problem only occurs on some hosting. – Red Hat Sep 08 '15 at 10:59
  • I have changed like this : $ck = tempnam('/cookies/',$data->username.'.txt'); and cookie saved in /tmp – Red Hat Sep 08 '15 at 12:07
0

http://curl.haxx.se/libcurl/c/CURLOPT_COOKIEJAR.html

Note that libcurl doesn't read any cookies from the cookie jar. If you want to read cookies from a file, use CURLOPT_COOKIEFILE.

AnyKey
  • 79
  • 2
0

When setting CURLOPT_COOKIEJAR, you need to use an absolute path. You can do this easily by using

curl_setopt ($ch, CURLOPT_COOKIEJAR, realpath($cookie_file) );

Answer stolen from: https://stackoverflow.com/a/19295661/161179

Community
  • 1
  • 1
gyaani_guy
  • 3,191
  • 8
  • 43
  • 51