cURL 不返回 JSON 文件

发布于 2024-12-05 17:54:37 字数 1471 浏览 0 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡忘如思 2024-12-12 17:54:37

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文