【发布时间】: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