当我在任何托管中使用jl登录json时,我无法保存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;
}
?>
答案 0 :(得分:0)
如果您的请求应该是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;
}
?>
答案 1 :(得分:0)
http://curl.haxx.se/libcurl/c/CURLOPT_COOKIEJAR.html
请注意,libcurl不会从cookie jar中读取任何cookie。如果你 想要从文件中读取cookie,请使用CURLOPT_COOKIEFILE。
答案 2 :(得分:0)
设置CURLOPT_COOKIEJAR时,需要使用绝对路径。您可以使用
轻松完成此操作 curl_setopt ($ch, CURLOPT_COOKIEJAR, realpath($cookie_file) );