cURL 不返回 JSON 文件
我有一个使用 cURL 的 PHP 脚本,我想用它从远程服务器检索 json 文件。要访问 json 文件/REST 系统,您需要使用身份验证。基本上,我可以让它登录并存储cookie。但是当我尝试抓取 json 页面并回显它时,它返回“未找到会话”。这是我的代码:
function login($url,$data) {
$fp = fopen("cookie.txt", "w");
fclose($fp);
$login = curl_init();
curl_setopt($login, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($login, CURLOPT_TIMEOUT, 10000);
curl_setopt($login, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($login, CURLOPT_URL, $url);
curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($login, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($login, CURLOPT_POST, TRUE);
curl_setopt($login, CURLOPT_POSTFIELDS, $data);
// ob_start();
return curl_exec ($login);
// ob_end_clean();
curl_close ($login);
//unset($login);
}
function grab_page($site){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_TIMEOUT, 40);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_URL, $site);
//ob_start();
echo curl_exec ($ch);
//ob_end_clean();
curl_close ($ch);
}
// Login and retrieve the my meteor page
login('https://www.mymeteor.ie','username=username-removed9&userpass=pass-removed');
echo grab_page('https://www.mymeteor.ie/cfusion/meteor/Meteor_REST/service/prepayBalance');
任何人都可以帮助我吗?
I have a PHP script using cURL that I want to use to retrieve a json file from a remote server. To get to the json file/REST system you need to use authentatication. Basically, I can get it to login and store cookies. But when I try to grab the json page and echo it, it returns "Session is not found". Here is my code:
function login($url,$data) {
$fp = fopen("cookie.txt", "w");
fclose($fp);
$login = curl_init();
curl_setopt($login, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($login, CURLOPT_TIMEOUT, 10000);
curl_setopt($login, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($login, CURLOPT_URL, $url);
curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($login, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($login, CURLOPT_POST, TRUE);
curl_setopt($login, CURLOPT_POSTFIELDS, $data);
// ob_start();
return curl_exec ($login);
// ob_end_clean();
curl_close ($login);
//unset($login);
}
function grab_page($site){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_TIMEOUT, 40);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_URL, $site);
//ob_start();
echo curl_exec ($ch);
//ob_end_clean();
curl_close ($ch);
}
// Login and retrieve the my meteor page
login('https://www.mymeteor.ie','username=username-removed9&userpass=pass-removed');
echo grab_page('https://www.mymeteor.ie/cfusion/meteor/Meteor_REST/service/prepayBalance');
Can anyone help me with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CURLOPT_COOKIEJAR
是在请求后放置 cookie 的地方 - 存储在那里以供以后使用。当需要随请求一起发送 cookie 时,请使用 CURLOPT_COOKIEFILE 指定要发送哪个 cookie。
您的
grab_page()
函数将需要使用后一个选项,而不是(或者,如果您需要在响应中存储任何 cookie 供以后使用)CURLOPT_COOKIEJAR
。The
CURLOPT_COOKIEJAR
is the place where the cookie is placed after a request — stored there for later use.When the time comes for sending a cookie along with a request, then use
CURLOPT_COOKIEFILE
to specify which cookie to send.Your
grab_page()
function will need to use this latter option, rather than (or as well as, if you need to store any cookies in the response for later)CURLOPT_COOKIEJAR
.