PHP post请求与cURL和cookie [英] PHP post request with cURL and cookie

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

问题描述

我试图向需要登录的网页发出请求。
我成功地取得了SESSID的cookie,并把它写入一个curl文件:

I am trying to make request to a webpage which requires a login. I successfully take the cookie with the SESSID and write it to a file with curl:

$username = 'xxx';
$password = 'xxxxxxx';
$url = 'http://example.com';
$cookie="cookie.txt";
$postdata = "username=$username&userpass=$password&autologin=1&userlogin=Login";

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url);

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);

curl_close($ch);

现在,我想使用cookie向网站发出POST请求,它使用API​​,如果你请求像
site.com/API/command.get(x) - 返回JSON信息 - 只是一个例子。所以如果你在浏览器中打开这个,它会返回JSON数据。

Now after this I want to make a POST request to the site with the cookie so it recognizes me as logged in. It uses API that returns JSON information if you do request like: site.com/API/command.get(x) - just an example. So if you open this in the browser, it returns the JSON data.

任何人都可以帮助我一个方法,怎么办?我是新的卷曲。

Can anyone help me with a way, how to do this? I am new to curl.

无论我尝试,我被重定向到登录页面。
尝试传递 curl_setopt($ ch,CURLOPT_COOKIEFILE,$ cookie); ,未成功。

No matter what I try, I get redirected to login page. Tried to pass curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);, no success.

推荐答案

感谢大家的提示。
我如何使用这个代码(我实际上使用了错误的帖子数据):

Thanks guys for your hints. How I got it working is with this code(I actually had used wrong post data):

$username = 'xxx';
$password = 'xxxx';
$url = 'http://example.com'; //request to the page i want the content from
$cookie="cookie.txt";

$url1 = "http://example.com/command..";

//login form action url
$postinfo = "act=login&login=$username&pass=$password";

$cookie_file_path = "cookie.txt";

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
//set the cookie the site has for certain features, this is optional
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_USERAGENT,
    "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);

$result = curl_exec($ch);
//var_dump($result);

//page with the content I want to grab
curl_setopt($ch, CURLOPT_URL, $url1);
//do stuff with the info with DomDocument() etc
$html = curl_exec($ch);
var_dump($html);
curl_close($ch);

我有这个代码在这里(stackoverflow)。感谢那个人发布了!不记得确切的名字。这个作品就像一个魅力!

I got this code somewhere in here(stackoverflow). Thanks to the guy that posted it! don't remember the exact name. This works like a charm !

这篇关于PHP post请求与cURL和cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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