2016-09-15 88 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) 

    ?>