如何使用 PHP 访问 Atlassian

How to access Atlassian using PHP

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

URL 通常看起来像:

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

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

到目前为止我只收到 401 授权请求错误。

我浏览了 Whosebug,我得到的只是如何访问基本 atlassian.com 和 bitbucket.org 页面。

是的,当然可以使用 PHP 和 cURL 访问 Atlassian 产品。我一直这样做是为了 create/modify Jira 问题

您将需要 find/write 一个库(或一组库)来访问 REST API 调用。在我的例子中,我写了一个基本的 REST 库,然后可以继承它来创建 Jira、Confluence、任何其他 REST 服务

搜索 Atlassian 文档站点以查找您正在使用的产品的 REST API(我猜您的情况是 Confluence)

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

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

使用 php 中的这段代码,您可以创建 Confluence 页面:

<?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*

标记了单词
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);