2013-02-07 76 views
0

我很接近這個工作,但每次我嘗試創建一棵樹時,都會收到服務器錯誤。有任何想法嗎?這裏是我的PHP代碼:github api v3創建樹php

function send_data($url, $content) { 
    $ch = curl_init(); 
    $timeout = 5; 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
    curl_setopt($ch, CURLOPT_USERPWD, 'myuser:mypass'); 
    curl_setopt($ch, CURLOPT_POST, TRUE); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($content)); 
    $a = array(); 
    $a['d'] = curl_exec($ch); 
    $a['i'] = curl_getinfo($ch); 
    curl_close($ch); 
    return $a; 
} 

$treeArr = array(
    "base_tree" => "d7126bd6c559ab461e851e96ef2c33675d851c5e", 
    "tree" => array(
     "path" => "resources/blahTest.txt", 
     "mode" => "100644", 
     "type" => "blob", 
     "sha" => "38d15319d3ee8a7292be0ec0da65fe111660a94d" 
    ) 
); 

$x = send_data("https://api.github.com/repos/srolfe26/Branch-IDE/git/trees",$treeArr); 
print_r($x); 

我提供BLOB的沙是一個新創建的blob我用同樣SEND_DATA功能做。 base_tree sha是從基本提交中找到的樹。另外,我在這裏跟隨這個例子:http://www.pqpq.de/2011/07/pithub-how-to-commit-new-file-via.html

謝謝!

回答

0

請求數據中的「樹」項需要是一個項目數組而不是單個項目,因爲樹是項目的集合。

$treeArr = array(
    "base_tree" => "d7126bd6c559ab461e851e96ef2c33675d851c5e", 
    "tree" => array(
     array(
      "path" => "resources/blahTest.txt", 
      "mode" => "100644", 
      "type" => "blob", 
      "sha" => "38d15319d3ee8a7292be0ec0da65fe111660a94d" 
     ) 
    ) 
); 
+0

我會試試看。我有一段時間沒有觸及這一點,但也許是時候完成我的原始過程了。 – runfaj