使用CURL发送cookie以及获得返回的cookie

一个简单的curl函数:

/**
 * 发送数据
 * @param String $url     请求的地址
 * @param Array  $header  自定义的header数据
								 $header = array('x:y','language:zh','region:GZ');
 * @param Array  $content POST的数据
								 $content = array('name' => 'wumian');
 * @param Array  $backHeader 返回数据是否返回header
								0不反回 1返回
 * @param Array  $cookie 携带的cookie
 * @return String
 */
function tocurl($url, $header, $content=array(),$backHeader=0,$cookie=''){
    $ch = curl_init();
    if(substr($url,0,5)=='https'){
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在
    }
    if(!isset($header[0])){//将索引数组转为键值数组
      foreach($header as $hk=>$hv){
         unset($header[$hk]);
         $header[]=$hk.':'.$hv;
      }
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POST, true);
    if(count($content)){
      curl_setopt($ch, CURLOPT_POSTFIELDS,$content);
    }
    if(!empty($cookie)){
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    }
    curl_setopt($ch, CURLOPT_HEADER,$backHeader); // 显示返回的Header区域内容
    $response = curl_exec($ch);
    if($error=curl_error($ch)){
        die($error);
    }
    curl_close($ch);
    return $response;
}

这里面重点说的有两点

  1. 获取返回的cookie
  2. 获取返回的body

获取COOKIE

方法有多种 只说我暂时使用的这种

CURLOPT_HEADER设置为true 这样 curl_exec执行后返回的内容里面就携带有header;我们知道cookie就是存在于header中的

我们通过正则匹配出返回的cookie

$r = tocurl('https://www.x.com/index.php?act=account&op=send_code','',$send,1);
$preg_cookie = '/Set-Cookie: (.*?);/m';
if(preg_match_all($preg_cookie,$r,$cookie)){
    $cookie = implode(';', $cookie['1']);
}
//$cookie的结果最终是这样的形式: x=1;y=2;  其中x,y是cookie的键 数字1 数字2为值

将这个cookie存储起来  想什么时候用就直接用了

获取BODY

$r = tocurl('https://www.X.com/index.php?act=account&op=send_code','',$send,1);
$preg_json = '/{(.*?)}/m';
if(preg_match($preg_json,$r,$json)){
    $json = $json['0'];
}

我这个获取比较有局限性 ;因为我知道请求的地址是返回json这个的;这个正则可以根据需求来写;

网上获取body的方法是这样的

 list($header, $body) = explode("\r\n\r\n", $r, 2);

但是这个使用了没有效果 所以最终根据实际需求 写个正则匹配了;

使用COOKIE

使用cookie也很简单

 curl_setopt($ch, CURLOPT_COOKIE, $cookie);//$cookie格式 x=1;y=2

这样对方就可以使用$_COOKIE['X']获取到值了

发布了71 篇原创文章 · 获赞 24 · 访问量 28万+
展开阅读全文

使用cURL PHP从页面获取所有cookie

06-11

<div class="post-text" itemprop="text"> <p><img src="https://i.stack.imgur.com/yuwcs.jpg" alt="enter image description here"> I'm trying to access a page using PHP's cURL, this page is to return 2 cookies in the "Response Headers", but in the cURL header only returns the first one. </p> <p>Does anyone know if there might be some block on the page, and how to work around this problem?</p> <blockquote> <p>This cookie is generated on a "Status Code: 302"</p> </blockquote> <p>I've already tried to get "file_get_contents" and also tried to do the <code>"CURLOPT_HEADERFUNCTION"</code> function and I also know success OBS: The function to break the google captcha is already working.</p> <pre><code>$url = 'https://pje.trt15.jus.br/captcha/login_post.php'; $post = [ 'g-recaptcha-response' => $g_response, 'referer' => '/consultaprocessual/pages/consultas/ConsultaProcessual.seam', 'random' => $g_captcha_random, 'entrar' => $g_captcha_entrar ]; if( $g_captcha_enviar != null ) $post[$g_captcha_enviar] = 'Enviar'; if( $g_captcha_entrar != null ) $post['entrar'] = $g_captcha_entrar; $post_http = http_build_query($post); $headers = [ ':authority: pje.trt15.jus.br', ':method: POST', ':path: /captcha/login_post.php', ':scheme: https', 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3', //'accept-encoding: gzip, deflate, br', 'accept-language: pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7', 'cache-control: no-cache', 'content-length: ' . strlen($post_http), 'content-type: application/x-www-form-urlencoded', 'cookie: '.$cookie_captchasess.' _ga=GA1.3.1830456077.1560269951; _gid=GA1.3.249690674.1560269951', 'origin: https://pje.trt15.jus.br', 'referer: https://pje.trt15.jus.br/consultaprocessual/pages/consultas/ConsultaProcessual.seam', 'upgrade-insecure-requests: 1' ]; $options = [ CURLOPT_COOKIESESSION => true, CURLOPT_HEADER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $post_http, CURLOPT_HTTPHEADER => $headers, CURLOPT_COOKIE => $cookie_captchasess, CURLOPT_FRESH_CONNECT => true, CURLOPT_VERBOSE => true, CURLOPT_REDIR_PROTOCOLS => CURLPROTO_HTTPS, CURLOPT_ENCODING => 'gzip, deflate', CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_AUTOREFERER => true, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_TIMEOUT => 40, CURLOPT_USERAGENT => $user_agent, ]; $ch = curl_init(); curl_setopt_array($ch, $options); $resp = curl_exec($ch); curl_close($ch); echo "<pre>"; echo $resp; </code></pre> </div> 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览