【问题标题】:curl json does not save cookiefile PHPcurl json不保存cookiefile PHP
【发布时间】:2015-12-03 22:58:46
【问题描述】:

当我在任何主机中使用带有 json 的 curl 登录时,我无法保存 cookiefile。这是我的代码:

<?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;	
		}
?>
我有 chmod 0777 目录 /cookies/。

【问题讨论】:

    标签: php json curl


    【解决方案1】:

    设置 CURLOPT_COOKIEJAR 时,需要使用绝对路径。您可以使用

    轻松做到这一点

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

    答案来自: https://stackoverflow.com/a/19295661/161179

    【讨论】:

      【解决方案2】:

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

      请注意,libcurl 不会从 cookie jar 中读取任何 cookie。如果你 要从文件中读取 cookie,请使用 CURLOPT_COOKIEFILE。

      【讨论】:

        【解决方案3】:

        如果您的请求应该是 POST 请求,则您没有指示 curl 发送 POST 请求。以下与您的函数略有不同,因为该函数没有 $cookie 参数 - curl 请求将使用系统临时目录来存储 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; 
            }
        ?>      
        

        【讨论】:

        • 我的问题是没有保存 cookie。我想保存一个 cookie 文件以再次卷曲。我测试了你的代码它工作但没有保存 cookie 文件。此问题仅在某些主机上出现。
        • 我已经改成这样了:$ck = tempnam('/cookies/',$data->username.'.txt');和 cookie 保存在 /tmp
        猜你喜欢
        • 2013-10-18
        • 2011-10-09
        • 1970-01-01
        • 1970-01-01
        • 2011-12-14
        • 1970-01-01
        • 2010-11-03
        • 1970-01-01
        • 2016-11-24
        相关资源
        最近更新 更多