我需要发送带身份验证的帖子请求。
我尝试使用此代码,但收到错误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或cookies我尝试但我无法弄清楚该怎么做。我要拔掉头发。请一些帮助非常感谢。
答案 0 :(得分:0)
状态代码302
表示网址重定向到另一个网址。将curl选项CURLOPT_FOLLOWLOCATION
设置为true以遵循此重定向。
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
答案 1 :(得分:0)
请尝试将POST数据作为数组传递。
$postData = array('Prepagata_codice' => 'Test', 'Prepagata_password' => '1234');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);