How to create and update a file in a Github repository with PHP and Github API?

前端 未结 3 1065
独厮守ぢ
独厮守ぢ 2021-01-13 16:12

I am trying to build PHP functions to create and update files within a Github repository, using PHP through the Github API.

The PHP files are being run from a s

相关标签:
3条回答
  • 2021-01-13 16:50

    Looks like research paid off and I can answer a main part of my own question (which I was not expecting to be able to do) - creating a new file. It looks like the same approach will be useable to update an exisiting file (if not, I'll be back).

    This link helped: http://www.lornajane.net/posts/2009/putting-data-fields-with-php-curl

    I realised the path referred to in Github API docs (https://developer.github.com/v3/repos/contents/#get-contents) - section Create a File - could be used as the url for curl as:

    https://api.github.com/repos/ USER / REPO_NAME /contents/ FILENAME
    

    And some tweaks were needed to allow for JSON to be sent. So I now have:

    $curl_url = $url
    $curl_token_auth = 'Authorization: token ' . $token;
    $ch = curl_init($curl_url);
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'User-Agent: $username', $curl_token_auth ));
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
    
    $response = curl_exec($ch);  
    curl_close($ch);
    $response = json_decode($response);
    
    return $response;
    

    For the $data, I used the JSON example from the Github API page:

    {
      "message": "my commit message",
      "committer": {
        "name": "Mike A",
        "email": "someemail@gmail.com"
      },
      "content": "bXkgbmV3IGZpbGUgY29udGVudHM="
    }
    

    After implementing this within a PHP file (on shared hosting), no other frameworks used, I saw the new file in the Github respository.

    0 讨论(0)
  • 2021-01-13 17:05

    Maybe it will help to Someone(With SHA feature in addition to update or delete files):

    <?php
    $file_git = "wall.jpg";
    $data_git = array(
    'sha'=>file_get_contents("sha.txt"),
    'message'=>'image',
    'content'=> base64_encode($file_git),
    'committer'=> array(
    'name'=>'Jacob',
    'email' => '45331093+greenandgreen@users.noreply.github.com'
    )
    );
    $data_string_git = json_encode($data_git);
    $ch_git = curl_init('https://api.github.com/repos/YOUR_REPO/contents/wall.jpg');
    curl_setopt($ch_git, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch_git, CURLOPT_POSTFIELDS, $data_string_git);
    curl_setopt($ch_git, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch_git, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 YaBrowser/19.9.3.314 Yowser/2.5 Safari/537.36',
    'Authorization: token PLACE_YOUR_PERSONAL_TOKEN_HERE'
    ));
    $result_git = curl_exec($ch_git);
    echo $result_git;
    $p_git = json_decode($result_git);
    file_put_contents("sha.txt",$p_git->content->sha);
    ?>
    

    Tested on PHP 7. If the code is working, then I wouldn’t refuse likes, in gratitude for the little work :)

    0 讨论(0)
  • 2021-01-13 17:15
    <?php
      function pushFile($username,$token,$repo,$branch,$path,$b64data){
        $message = "Automated update";
        $ch = curl_init("https://api.github.com/repos/$repo/branches/$branch");
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent:Php/Automated'));
        curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $token);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $data = curl_exec($ch);
        curl_close($ch);
        $data=json_decode($data,1);
    
        $ch2 = curl_init($data['commit']['commit']['tree']['url']);
        curl_setopt($ch2, CURLOPT_HTTPHEADER, array('User-Agent:Php/Ayan Dhara'));
        curl_setopt($ch2, CURLOPT_USERPWD, $username . ":" . $token);
        curl_setopt($ch2, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch2, CURLOPT_RETURNTRANSFER, TRUE);
        $data2 = curl_exec($ch2);
        curl_close($ch2);
        $data2=json_decode($data2,1);
    
        $sha='';
        foreach($data2["tree"] as $file)
          if($file["path"]==$path)
            $sha=$file["sha"];
        
        $inputdata =[];
        $inputdata["path"]=$path;
        $inputdata["branch"]=$branch;
        $inputdata["message"]=$message;
        $inputdata["content"]=$b64data;
        $inputdata["sha"]=$sha;
    
        echo json_encode($inputdata);
    
        $updateUrl="https://api.github.com/repos/$repo/contents/$path";
        echo $updateUrl;
        $ch3 = curl_init($updateUrl);
        curl_setopt($ch3, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', 'User-Agent:Php/Ayan Dhara'));
        curl_setopt($ch3, CURLOPT_USERPWD, $username . ":" . $token);
        curl_setopt($ch3, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch3, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch3, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch3, CURLOPT_POSTFIELDS, json_encode($inputdata));
        $data3 = curl_exec($ch3);
        curl_close($ch3);
    
        echo $data3;
      }
      //pushFile("your_username","your_personal_token","username/repository","repository_branch","path_of_targetfile_in_repository","base64_encoded_data");
    ?>
    
    0 讨论(0)
提交回复
热议问题