2016-09-15 70 views
0

我能夠獲取GET請求,但在POST和PUT請求中存在與驗證有關的問題。我收到錯誤「您必須先登錄才能使用Bugzilla的這一部分」。我提供了正確的用戶名和密碼。我已經嘗試CURLAUTH_ANY以及CURLAUTH_BASIC。我已經嘗試了PUT和POST請求。任何幫助表示讚賞。您必須在使用Bugzilla的這部分代碼之前登錄,代碼:410

$url ="http://localhost:8080/bugzilla/rest/bug/2"; 
    $apikey = "IZC4rs2gstCal0jEZosFjDBRV9AQv2gF0udh4hgq"; 
    $data = array(
      "product" => "TestProduct", 
      "component" => "TestComponent", 
      "version" => "unspecified", 
      "summary" => "This is a test bug - please disregard", 
      "alias" => "SomeAlias", 
      "op_sys" => "All", 
      "priority" => "P1", 
      "rep_platform" => "All" 
     ); 

    $str_data = json_encode($data); 

    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS,$str_data); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, 
       array("Content-Type: application/json", "Accept: application/json")); 
$username = '[email protected]'; 
$password = 'abbincrc'; 
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
$result = curl_exec($ch); 
$info = curl_getinfo($ch); 
curl_close($ch); 

echo $result 
+0

您正在聲明API密鑰變量但未使用它。 – jedifans

+0

'localhost:8080'? – RamRaider

+0

localhost:8080是正確的(沒有問題,因爲我的Bugzilla被配置爲在端口8080運行)。我可以從瀏覽器調用它,一切正常,我沒有使用API​​密鑰,我剛剛宣佈它(我只是試驗)。我在網上看到的例子不需要API密鑰,只有用戶名和密碼就足夠了。感謝您的評論 – user1468768

回答

0

以下代碼解決了我的問題。我有written a blog on it這可能會對遇到同樣問題的其他人有用。

 <?php 

     $url = 'http://localhost:8080//bugzilla/xmlrpc.cgi'; 
     $ch = curl_init(); 

     $header = array(
      CURLOPT_URL  => $url, 
      CURLOPT_POST => true, 
      CURLOPT_RETURNTRANSFER => true, 
      CURLOPT_HTTPHEADER => array('Content-Type: text/xml', 'charset=utf-8') 
     ); 
     curl_setopt_array($ch, $header); 

     $bugreport = array(
      'login'  => '[email protected]', 
      'password' => 'abbincrc', 
      'product'  => "TestProduct", 
      'component' => "TestComponent", 
      'summary'  => "Bug Title : A One Line Summary", 
      'assigned_to' => "[email protected]", 
      'version'  => "unspecified", 
      'description' => "Bug Description : A Detailed Problem Description", 
      'op_sys'  => "All", 
      'platform' => "All", 
      'priority' => "Normal", 
      'severity' => "Trivial" 
     ); 

     $request = xmlrpc_encode_request("Bug.create", $bugreport); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $request); 
     curl_exec($ch) 

    ?> 
相關問題