如何使用PHP访问Atlassian

时间:2015-08-24 11:29:42

标签: php curl confluence

我想使用用户名和密码从我的Atlassian获取内容。

网址通常如下:

http://my-own-site.atlassian.net/wiki/pages/viewpage.action?spaceKey=TO&title=Any-Wiki-Title

是否可以使用 PHP CURL 从此页面获取内容?

到目前为止,我只收到401 auth reqd错误。

我查看了Stackoverflow,我得到的是如何访问基本的atlassian.com和bitbucket.org页面。

3 个答案:

答案 0 :(得分:1)

是的,使用PHP和cURL访问Atlassian产品当然是可能的。我一直都在创建/修改Jira问题

您必须查找/编写一个库(或一组库),这将允许您访问REST API调用。在我的例子中,我写了一个基础REST库,然后可以继承它来创建Jira,Confluence,任何其他REST服务

搜索Atlassian文档网站,找到您正在使用的产品的REST API(我猜你的情况下是Confluence)

不要忘记REST API使用GET,POST,PUT和DELETE方法,因此您的库需要处理所有这些

关于您的错误,我 * think * 您的登录信息需要被允许访问API调用

答案 1 :(得分:1)

在php中使用此代码,您可以创建Confluence Pages:

<?php

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "http://localhost:8090/rest/api/content/");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"type\":\"page\",\"title\":\"**inserttitle**\",\"space\":{\"key\":\"**insertspace**\"},\"ancestors\":[{\"type\":\"page\",\"id\":**insertancestor**}],\"body\":{\"storage\":{\"value\":\"<p>This is a new page</p>\",\"representation\":\"storage\"}}}");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_USERPWD, "**insertusername**" . ":" . "**insertpassword**");

    $headers = array();
    $headers[] = "Content-Type: application/json";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close ($ch);

    ?>

使用此代码,您可以从Confluence获取内容:

<?php

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://localhost:8090/rest/api/content/**insertid**");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_USERPWD, "**insertusername**" . ":" . "**insertpassword**");
    $headers = array();
    $headers[] = "Content-Type: application/json";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close ($ch);   

?>



   echo $result;

修改参数。我用insert *

标记了这些单词

答案 2 :(得分:1)

To retrieve any existing content properties for a piece of content, use url as https://your-domain.atlassian.net/wiki/rest/api/content/{content_ID}
$curl = curl_init();
$post = array(
    "id" => "{content_ID}",
    "type" => "{content_ID}", //Ex. page
    "title" => "{content_Title}",
    "space" => ["key" => "{spaces_key}"],
    "body" => ["storage" => ["value" => "<p>Here comes the other text</p>", "representation" => "storage"]],
        "version" => ["number" => 15]
    );
curl_setopt_array($curl, array(
    CURLOPT_URL => "https://your-domain.atlassian.net/wiki/rest/api/content/{content_ID}?expand=metadata.properties.myprop,space,body.view,version,container",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 300,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_POSTFIELDS => json_encode($post),
    CURLOPT_HTTPHEADER => array(
        "authorization: Basic {base64 of (username:password)}",
        "content-type: application/json",
        'Accept: application/json'
    ),
));
$result = curl_exec($curl);
curl_close($curl);