2015-10-13 92 views
1

我正在使用PHP中的基本REST應用程序,並且我有以下代碼。 客戶端代碼:通過CURL PUT傳遞PHP數組會生成通知未定義索引

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 
curl_setopt($ch , CURLOPT_HEADER,false); 
curl_setopt($ch , CURLOPT_FOLLOWLOCATION , true); 
curl_setopt($ch , CURLOPT_URL , "http://localhost/Hello/Rest"); 
curl_setopt($ch , CURLOPT_POSTFIELDS,  
        http_build_query(array("username"  => "test"))); 
$output = curl_exec($ch); 
print_r($output); 
curl_close($ch); 

服務器端代碼:

$username = $_REQUEST['username']; 
echo "This is a PUT request"; 
echo "<br>"; 
echo $username; 
echo "<br>"; 

出於某種原因,$ _REQUEST [ '用戶名']是不是recognized.Generates不確定的指數。

不太確定是什麼導致了這個錯誤。

+0

你可以var_dump整個請求? – ogres

+0

@ogres:我做的是空的。我認爲我們傳遞參數的方式是錯誤的。 – kta

+0

你也可以var_dump $ _SERVER並檢查請求本身嗎? – ogres

回答

4

由於@NaijaProgrammer指出,$ _REQUEST不包含PUT值。如果你想堅持使用PUT,你需要修改你的服務器代碼。看到這個鏈接for more information.

// for put requests 
parse_str(file_get_contents("php://input"),$post_vars); 
$username = $post_vars['username']; 
echo "This is a PUT request"; 
echo "<br>"; 
echo $username; 
echo "<br>"; 
+0

非常感謝你的答案,它解決了問題。Cheers Mate.LUV SO。 – kta

+0

很高興幫助。你可能會考慮接受它;-) – Jan

-4

你必須回到你的$輸出

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 
curl_setopt($ch , CURLOPT_HEADER,false); 
curl_setopt($ch , CURLOPT_FOLLOWLOCATION , true); 
curl_setopt($ch , CURLOPT_URL , "http://localhost/Hello/Rest"); 
curl_setopt($ch , CURLOPT_POSTFIELDS,  
       http_build_query(array("username"  => "test"))); 
$output = curl_exec($ch); 
curl_close ($ch); 
return $output; 
+1

這是無稽之談。 – Jan

+0

返回'curl_exec'的輸出將無濟於事。閱讀文檔http://php.net/manual/en/function.curl-exec.php – Eborbob