【问题标题】:PHP CURL Authentication error code 302PHP CURL 身份验证错误代码 302
【发布时间】:2017-07-28 16:42:24
【问题描述】:

我需要发送带有身份验证的发布请求。

我尝试使用此代码,但收到错误 302。

$url = "https://wifinext.internavigare.com/prepagataAnagrafica/creaUtente/";
$username = 'myuser';
$password = 'mypassword';
// create a new cURL resource
$ch= curl_init($url);

// do a POST request, using application/x-www-form-urlencoded type
curl_setopt($ch, CURLOPT_POST, TRUE);
// credentials
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
// returns the response instead of displaying it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

//Set Post Fields
curl_setopt($ch, CURLOPT_POSTFIELDS, "Prepagata_codice=Test&Prepagata_password=123456");

// do request, the response text is available in $response
$response = curl_exec($ch);
// status code, for example, 200
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

//show response
echo $statusCode;
echo $response;

// close cURL resource, and free up system resources
curl_close($ch);

我看到一些答案说要使用 CURLOPT_FOLLOWLOCATION 或 cookie,我尝试了一下,但我不知道该怎么做。我正要拔头发。非常感谢您的帮助。

【问题讨论】:

  • 这里也一样!有人知道如何解决这个问题吗?我想通过 url 向另一个 php 文件发出请求。但是我得到了相同的 302 http 代码

标签: php authentication curl


【解决方案1】:

状态码302 表示网址重定向到另一个网址。将 curl 选项 CURLOPT_FOLLOWLOCATION 设置为 true 以遵循此重定向。

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

【讨论】:

  • 感谢您的回答我试了一下,但随后显示登录页面。我不知道之后我要做什么。
  • 这取决于你在做什么。我不知道您的应用程序的功能是什么。如果您还有其他问题,请将此问题标记为已解决并创建一个新问题。
  • 从我发布的代码中可以看出,我需要将此帖子字段发送到页面 curl_setopt($ch, CURLOPT_POSTFIELDS, "Prepagata_codice=Test&Prepagata_password=123456");但是,如果我使用 CURLOPT_FOLLOWLOCATION 接缝,它只会带到登录页面而不是进行身份验证
  • 感谢您抽出宝贵时间,非常感谢您的努力,但我无法将答案标记为已解决,因为正如我在帖子中所写,我已经能够添加 CURLOPT_FOLLOWLOCATION 但我的问题是只有这个接缝不进行身份验证,我不知道重定向后我必须做什么
【解决方案2】:

请尝试将 POST 数据作为数组传递。

$postData = array('Prepagata_codice' => 'Test', 'Prepagata_password' => '1234');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    相关资源
    最近更新 更多