How can I Create, update and delete github code (file) programatically using php (preferably curl) and github api without using any external library ?
I cannot find anything about this. Is this possible or not ?
Probably the safest/most proper solution would be to use the Github API. You can find the documentation on creating a commit here: https://developer.github.com/v3/git/commits/#create-a-commit
I know you're not wanting to use a library, but this library would probably make this a lot easier: https://packagist.org/packages/knplabs/github-api
If you're not using a library, you'll need to figure out how to make sure your request is properly authenticated: https://developer.github.com/v3/#authentication
Finally I've resolved it.
<?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");
?>
Related
I am facing varied issue. I am able to get response in POSTman but getting below error while using PHP code.
You are not authorized to access this resource
code as below:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml','Accept: application/xml'));
$data = curl_exec($ch);
curl_close($ch);
Unfortunately, different cURL versions behave slightly different and so there is not one valid answer but several approaches that work for different cURL versions.
Here are two suggestions:
From Problems with username or pass with colon when setting CURLOPT_USERPWD
Try adding curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);, or instead CURLAUTH_BASIC.
Something that should always work:
If it won't help, add username and password directly into url like https://user:pass#host.com/path.
You shouldnt turn off certificate verification, instead, get a valid cert, they are for free using letsencrypt.
<?php
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . base64_encode($password)); //here is the change
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml','Accept: application/xml'));
$data = curl_exec($ch);
curl_close($ch);
This is a really longshot and i know that but i have seen quite a few API's that work like that and since the OP seems not to have the documentation of the API i will post this as an answer in case it helps him solve his issue.
If above does not work try to base64_encode($username) as well
I have a problem, I tried updating JSON formated data with CURL to my online database: Firebase. It works fine at first, but when I send Data witch I have sent somewhen before it doesn´t get stored. I am sending a combination of a few GET-Variables.
if($_GET['user']!='')
{
$user_url = str_replace('.' , ',' , $_GET['user']);
$url='https://websitenew.firebaseio.com/' . $user_url . '.json';
$data = array('author'=>$_GET['user'], 'temp'=>$_GET['temp'], 'druck'=>$_GET['press'], 'notMoving'=>$_GET['move'], 'gpsx'=>$_GET['gpsx'], 'gpsy'=>$_GET['gpsy']);
$data_json = json_encode($data);
$header = array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_json));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_exec($ch);
curl_close($ch);
}
Thanks!
This should Ideally work with get Variable. If that does not work then try mapping it to a variable in php and then creating a JSON out of it. If the API does not work. Please try out the below one which I had used in my project and it is fully functioning too.
https://github.com/eldhosemjoy/firebase
I hope this git could help you understand the operations with firebase too.
I am using postmates API for my book store.
I am using their test credentials, but I don't know how to use it. Kindly guide me how can I make a basic api call.
I am using PHP. I want to send post data using cURL
Thank you
<?php
$url = "https://api.postmates.com/v1/customers/my-customer-key/delivery_quotes";
$uname = "my-api-key";
$pwd = null;
$data = array(
'dropoff_address' => '20 McAllister St, San Francisco, CA 94102',
'pickup_address' => '101 Market St, San Francisco, CA 94105',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$uname:$pwd");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
if (curl_errno($ch)) {
echo 'error:' . curl_error($ch);
}
curl_close($ch);
echo $output;
?>
From the documentation:
The Postmates API requires authentication by HTTP Basic Auth headers. Your API key should be included as the username. The password should be left empty.
This means you need to do something like the following:
$curl = curl_init();
$url = "http://whatever-this-should-be";
curl_setopt($curl, CURLOPT_URL, $url);
$username = "your-api-key-goes-here";
$password = ""; // leave this blank, as per the doc
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
$res = curl_exec($curl);
You'll need to tweak other curl options to handle output, etc. but this is the core of what you'll need.
You might also want to check out this library if you want to do all of this at a slightly higher (i.e. easier) level.
UPDATE: Please be aware that this is just sample code. I wouldn't advocate storing your API key in your script file for production code!
I have made this code and it is working fine. Thank you all for answering my question.
$url = "https://api.postmates.com/v1/customers/my-customer-id/delivery_quotes";
$uname = "my-api-key";
$pwd = null;
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded','Accept: application/json'));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $uname . ":" . $pwd);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, "dropoff_address=20 McAllister St, San Francisco, CA 94102&pickup_address=101 Market St, San Francisco, CA 94105");
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
$return = curl_exec($process);
curl_close($process);
var_dump($return);
Hey guys im trying to print one api using curl. but so far i wasnt able to get it to work. maybe you guys can help me. the website uses api user and pass both can be viewed in the code i made so far. what this does is it gets the $original_url and gives us a preview based on the template_id which is 20016. you can read the documentation here https://support.dudamobile.com/API/API-Use-Cases/Multiscreen-White-Label-Setup
$original_url = "http://rsportugal.org/index.html";
$data = array("template_id"=>"20016","url"=>$original_url);
$data = json_encode($data);
define("API_USER","...");
define("API_PASS","...");
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.dudamobile.com/api/sites/multiscreen/templates');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
$output = curl_exec($ch);
if(curl_errno($ch)) {
die('Curl error: ' . curl_error($ch));
}
$output = json_decode($output);
curl_close($ch);
return $output->site_name;
hopefully you guys can help me
It seems you want to do a http POST on an url that doesn't support POST.
If you want to get 20016 template data, you have to do so :
$templateId = 20016;
define("API_USER","...");
define("API_PASS","...");
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.dudamobile.com/api/sites/multiscreen/templates/' . $templateId);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$output = curl_exec($ch);
if(curl_errno($ch)) {
die('Curl error: ' . curl_error($ch));
}
$output = json_decode($output);
print_r($output);
curl_close($ch);
Full API documentation here => Duda mobile api Doc
So, I've been trying to execute the function Cron::add_line using an script that I have builded and it didn't work properly yet. The script login into cPanel but in the second part it don't execute the json-api to execute the function add_line, I've did many searches about this but never found anything to make me comprehend how it works in cURL.
If I type the command url in my browser, it works and create the cron, otherwise, using my script it doesn't work.
If anyone could help I appreciate!
Thanks!
Code:
<?php
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$site = 'https://' . $_REQUEST['site'] . ':2083';
$auth_url = $site . '/login/?';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $auth_url);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'user=' . $username . '&pass=' . $password);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100020);
$result = curl_exec($ch);
$h = curl_getinfo($ch);
if ($result == true && strpos($h['url'], "cpsess"))
{
$pattern = "/.*?(\/cpsess.*?)\/.*?/is";
$preg_res = preg_match($pattern, $h['url'], $cpsess);
}
$cpcmd_url = $site . "$ENV{'" . $cpsess[1] . "'}" . '/json-api/';
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $cpcmd_url);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'cpanel?user=$username&cpanel_jsonapi_module=Cron&cpanel_jsonapi_func=add_line&cpanel_jsonapi_version=2&command=GET http://www.cdihost.com.br/admin/cron.php&day=*&hour=*&minute=*/5&month=*&weekday=*');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100020);
$result = curl_exec($ch);
curl_close($ch);
?>
Try some debugging, like error_log($cpcmd_url); to see if the url is correct. Also, in CURLOPT_POSTFIELDS, cpanel? seems wrong to me.