如何使用PHP访问Atlassian

问题描述 投票:1回答:3

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

URL通常如下所示:

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页面。

php curl confluence
3个回答
1
投票

使用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 *标记了这些单词


1
投票

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

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

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

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

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


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);
© www.soinside.com 2019 - 2024. All rights reserved.