2017-07-28 62 views
0

我需要發送帶有身份驗證的發佈請求。PHP CURL身份驗證錯誤代碼302

我試試這個代碼,但我得到錯誤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或餅乾,我嘗試,但我想不出該怎麼辦。我正要拔出我的頭髮。請一些幫助,真的很感激。

回答

0

狀態碼302表示該URL重定向到另一個URL。將捲曲選項CURLOPT_FOLLOWLOCATION設置爲true以遵循此重定向。

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
+0

您的回答的問題我嘗試一下,但隨後顯示登錄頁面。我不知道我必須要做什麼。 – user2272143

+0

這取決於你在做什麼。我不知道你的應用程序的功能是什麼。如果您還有其他問題,請將此問題標記爲已解決並創建一個新問題。 – Jerodev

+0

正如你所看到的我發佈的代碼,我需要發送此帖子字段到頁面 curl_setopt($ ch,CURLOPT_POSTFIELDS,「Prepagata_codice = Test&Prepagata_password = 123456」); 但是,如果我使用CURLOPT_FOLLOWLOCATION縫,它只帶到登錄頁面,而不是驗證 – user2272143

0

請嘗試將POST數據作爲數組傳遞。

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