Php:发送cookie在curl请求 [英] Php : sending cookie in curl Request

查看:592
本文介绍了Php:发送cookie在curl请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下curl代码GET。

I have below curl code to GET.

<?php  
function get_content($URL){
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
          curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 
          curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); 
          curl_setopt($ch, CURLOPT_URL, $URL);
          $data = curl_exec($ch);
          curl_close($ch);
          return $data;
      }
 echo get_content("http://www.domain.com/cool.php");
?>

我已使用http标头和cookie如下所示

I have used http headers and cookie looks like below

xidseq:22
xid:b05f251c-8a72-4c2b-a230-e03b9c5c87b7&&BAYIDSAMFE1C013&343
data:dsfsfssdss

我需要发送GET请求到 http://www.domain.com/cool.php 与一些cookie。

I need to send GET request to http://www.domain.com/cool.php with some cookies.

如何把cookie放在cookie.txt?

how do i put the cookie in cookie.txt ?? is there any specific format for cookies..or just posting it works ?

推荐答案

如果上面的脚本不起作用,请尝试:

If script above doesn't work try:

curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookie.txt'); 
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt'); /* make sure you provide FULL PATH to cookie files*/

检查文件权限和所有权< c $ c> dirname(__ FILE__)。 '/cookie.txt'。

Check file permissions and ownership on dirname(__FILE__) . '/cookie.txt' . Make sure file is writable.

要将cookie放入cookie.txt,您需要访问包含这些Cookie的某些网页 - 只能抓取服务器端Cookie

To put the cookie in cookie.txt you need to visit certain web page that contains them - only server side cookies you can fetch with cURL, javascript cookies is not reachable vie cURL, at least not directly.

如果您想为GET请求手动创建Cookie,请参阅Netscape Cookie格式或 - 最佳方式,通过CURLOPT_COOKIEJAR保存一些真实网站Cookie,以查看和了解格式。

If you want to create cookies manually for your GET request, read about Netscape cookies format or - best way, save some 'real website' cookies via CURLOPT_COOKIEJAR to see and understand format.

这篇关于Php:发送cookie在curl请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆