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);
Related
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");
?>
I'm trying to get some data from an api that is protected by http authentication.
I'm want the response to be a json, but I'm getting a string innstead.
What can I do to fix this?
This is my code:
//init curl
$ch = curl_init();
$process = curl_init($url."?".$queryString);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/json', ''));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, '');
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
print_r($return);
I want to login using JIRA API with curl operation.
I having problem in curl operation. I have main URL in JIRA_URL.'/demo/111'; values for $username and $password are passed in the function correctly but shows the status 'failure'. Is any issues in my curl code
function JIRA_authenticate($username, $password) {
$url = JIRA_URL . '/demo/111';
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // ssl ensure cert
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); /// ssl ensure cert
$issue_list = (curl_exec($curl));
echo $issue_list;
return $issue_list;
}
You didn't mention what you are trying to achieve with this code.
Are you trying to get a ticket info, post an issue? You are just using the API...
Well here's a script that works with the JIRA API.
<?php
$username = 'test';
$password = 'test';
$url = "https://xxxxx.xxxxxxx.net/rest/api/2/project";
$ch = curl_init();
$headers = array(
'Accept: application/json',
'Content-Type: application/json'
);
$test = "This is the content of the custom field.";
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
//curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result = curl_exec($ch);
$ch_error = curl_error($ch);
if ($ch_error) {
echo "cURL Error: $ch_error";
} else {
echo $result;
}
curl_close($ch);
?>
This code is fetching a project from JIRA.
If you want to create issues, you will have to change the REST URL to /rest/api/2/issue/ and use "POST" instead of "GET" method.
Using PHP curl, i'm trying to post message from php application to basecamp. When i run this code, it returns the error like this
Hmm, that isn’t right
You may have typed the URL incorrectly.
Check to make sure you’ve got the spelling, capitalization, etc. exactly right.
Code
<?php
$username = 'username';
$password = 'password';
$datastring = json_encode(array("name" => "from cURL"));
$URL = "https://basecamp.com/***/api/v1/projects/****.json";
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $datastring);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
?>
Not sure the API url is correct or not? How it can solve?
The url doesn't look right - you'll want to check https://github.com/basecamp/bcx-api/blob/master/sections/messages.md#create-message and https://github.com/basecamp/bcx-api/blob/master/sections/comments.md#create-comment for adding a message or comment.
To add a new message to the project, you'd want
$URL = "https://basecamp.com/***/api/v1/projects/****/messages.json";
I'm trying to connect to the basecamp api (json) using PHP + cURL, but all my attempts have failed, and I'm not sure why. The end goal is to build a dashboard that displays information from basecamp projects to team members over http.
<?php
$basecamp_url = 'https://basecamp.com/xxxxxx/api/v1';
$username = 'username';
$password = 'pass';
$session = curl_init();
curl_setopt($session, CURLOPT_URL, $basecamp_url.'/projects.xml');
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_HTTPGET, 1);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session,CURLOPT_USERPWD,$username . ":" . $password);
$response = curl_exec($session);
curl_close($session);
echo '<pre>';
print_r($response);
?>
I am working on a very similar project as you are right now. You have to create a Basecamp app first. Than the following code should get you connected:
$appName = 'your app name';
$appContact = 'your app email;
$basecampAccountId = 'xxxxx';
$basecampUsername = 'youremailhere';
$basecampPassword = 'yourpassword here';
$baseUrl = "https://basecamp.com/$basecampAccountId/api/v1";
$url= $baseUrl.'/projects.json';
$credentials = "$basecampUsername:$basecampPassword";
$helloHeader = "User-Agent: $appName ($appContact)";
echo $url.'<br>';
echo $credentials.'<br>';
echo $helloHeader.'<br>';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, $credentials);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($helloHeader));
$response = curl_exec($ch);
$errno = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
print_r($response);