I can get data from the Trello API using this:
private function get_card_info($card_id) {
$client = new \GuzzleHttp\Client();
$base = $this->endpoint . $card_id;
$params = "?key=" . $this->api_key . "&token=" . $this->token;
$cardURL = $base . $params;
$membersURL = $base . "/members" . $params;
$attachmentsURL = $base . "/attachments" . $params;
$response = $client->get($cardURL);
$this->card_info['card'] = json_decode($response->getBody()->getContents());
$response = $client->get($membersURL);
$this->card_info['members'] = json_decode($response->getBody()->getContents());
$response = $client->get($attachmentsURL);
$this->card_info['attachments'] = json_decode($response->getBody()->getContents());
}
However, this is broken into three calls. Is there a way to get card information, the member information, and the attachment information all in one call? The docs mention using &fields=name,id, but that only seems to limit what's returned from the base call to the cards endpoint.
It's absurd to have to hit the API 3 times every time I need card information, but I can't find any examples gathering all that's needed.
Try hitting the API with following parameters:
/cards/[id]?fields=name,idList&members=true&member_fields=all&& attachments=true&&attachment_fields=all
Trello replied to me, and stated that they would have answered much like Vladimir did. However, the only response I got from that was the initial card data, sans attachments and members. However, they also directed me to this blog post that covers batching requests. They apparently removed it from the docs because of the confusion it created.
To summarize the changes, you essentially make a call to /batch, and append a urls GET parameter with a comma separated list of endpoints to hit. The working final version ended up looking like this:
private function get_card_info($card_id) {
$client = new \GuzzleHttp\Client();
$params = "&key=" . $this->api_key . "&token=" . $this->token;
$cardURL = "/cards/" . $card_id;
$members = "/cards/" . $card_id . "/members";
$attachmentsURL = "/cards/" . $card_id . "/attachments";
$urls = $this->endpoint . implode(',', [$cardURL, $members, $attachmentsURL]) . $params;
$response = $client->get($urls);
$this->card = json_decode($response->getBody()->getContents(), true);
}
Related
i want to generate the signature this code gives me using simple php with no oauth class:
$oauth = new OAuth($my_key, $my_secret);
$bodyHash = base64_encode(sha1($body_content, true)); //contains the body
$sig = $oauth->generateSignature('GET', $url, Array("oauth_body_hash" => $bodyHash));
what i have done so far is this:
$bodyHash = base64_encode(sha1($xml, true));
$result_data = array(
'oauth_body_hash' => $bodyHash
);
$result_data_keys = array_keys($result_data);
sort($result_data_keys);
$launch_params = array();
foreach ($result_data_keys as $key) {
array_push($launch_params, $key . "=" . rawurlencode($result_data[$key]));
}
$base_string = "POST&" . urlencode($sUrl) . "&" . rawurlencode(implode("&", $launch_params));
$signature = base64_encode(hash_hmac("sha1", $base_string, $my_secret, true));
but im not getting the same signature!
this is the simplified version of the problem i have posted earlier here: Building a body signed oauth xml request for LTI Outcomes service
any idea what im doing wrong?
I have a success to download a repository from github via file_get_content but what is the orientation with curl.
The advantage with the file_get_content it's like anonymous. In this case inside a backoffice, it's possible to download plugin in for example.
But Github restrict this usage at less 10 times, after the user must to wait long time to restart something.
It seems if the curl is used, github allow more access to download a respository.
Do you have an example to use with curl (like anonymous)?
Below an example that I make. It's works fine but it's not possible to use this several times. Think less 10 times.
Thank you
public function __construct() {
$this->githubUrl = 'https://github.com';
$this->githubApi = 'https://api.github.com';
$this->githubRepo = 'repos';
$this->context = stream_context_create(array('http' => array('header' => 'User-Agent: ClicShopping',)));
$this->githubRepoClicShoppingCore = 'CoreofApplication';
$this->githubRepoName = 'addOnNameOfApplication';
}
private function getGithubRepo() {
$url = $this->githubApi . '/' . $this->githubRepo . '/' . $this->githubRepoName;
return $url;
}
private function getGithubCoreRepo() {
$url = $this->githubApi . '/' . $this->githubRepo . '/' . $this->coreName . '/' . $this->githubRepoClicShoppingCore;
return $url;
}
private function setContext() {
return $this->context;
}
private function getGithubApiClicShoppingCoreArchive() {
$url = $this->githubUrl . '/' .$this->coreName . '/' . $this->githubRepoClicShoppingCore.'/archive/master.zip';
return $url;
}
Thank you.
I'm trying to add new users to moodle 3.2 using a REST web service, and i want to customize thees fields (phone1, department, institution) in the student profile.
I used this code
$token = 'a38805c00f33023f7854d5adc720c7a7';
$domainname = 'http://localhost/moodle';
$functionname = 'core_user_create_users';
$restformat = 'json';
$user2 = new stdClass();
$user2->username = strtolower( $rsnew['Serial']);
$user2->password = $rsnew['pass'];
$user2->firstname = $rsnew['Fname'];
$user2->lastname = $rsnew['Lname'];
$user2->email = $rsnew['Email'];
$user2->lang = 'en';
$user2->auth = 'manual';
$user2->country = $rsnew['Country'];
$user2->timezone = '99';
$user2->phone1 = $rsnew['phone'];
$user2->department = $rsnew['dept'];
$user2->institution = $rsnew['branch'];
$user2->idnumber = $rsnew['grade'];
$users = array($user2);
$params = array('users' => $users);
$serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' . $token . '&wsfunction='.$functionname;
require_once('./curl.php');
$curl = new curl;
$restformat = ($restformat == 'json')?'&moodlewsrestformat=' . $restformat:'';
$resp = $curl->post($serverurl . $restformat, $params);
But i get this error :
{
"exception": "invalid_parameter_exception",
"errorcode": "invalidparameter",
"message": "Invalid parameter value detected",
"debuginfo": "users => Invalid parameter value detected: Unexpected keys (phone1, department, institution) detected in parameter array."
}
What should I do to fix that?
Just as the error suggests, that web service does not support the fields you are giving it. You can refer to the function itself to find out what fields are supported and what data they must contain.
core_user_get_users
I am not aware of a workaround for your problem using existing web services. However, you can create your own which includes those additional fields.
Note that this sounds to me like this is a desirable feature and should be raised on the issue tracker.
I am using REST to call the Magento product list, but it is only showing 10 products instead of all of them.
Could anyone guide because of what this problem may occur?
This is the code I am using to call the rest api :-
$url = 'magentohost url';
$callbackUrl = $url . "oauth_admin.php";
$temporaryCredentialsRequestUrl = $url . "oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = $url . 'admin/oauth_authorize';
$accessTokenRequestUrl = $url . 'oauth/token';
$apiUrl = $url . 'api/rest';
$consumerKey = 'consumer_key';
$consumerSecret = 'consumer_secret';
$token = 'token';
$secret = 'token_secret';
try {
$oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
$oauthClient->setToken($token, $secret);
$resourceUrl = "$apiUrl/products";
$oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/json', 'Accept' => 'application/json'));
$productsList = json_decode($oauthClient->getLastResponse());
echo '<pre>';
print_r($productsList);
}
catch(Exception $e) {
echo '<pre>';
print_r($e);
}
It is giving me proper output, but never more than 10 products, when I want to output all of them.
By Default the limit is 10. You can pass the limit as follows:
You can define the limit of items returned in the response by passing the limit parameter. By default, 10 items are returned and the maximum number is 100 items. You can also define the page number by passing the page parameter. Example:
http://magentohost/api/rest/products?page=2&limit=20
I would like to add from the comment above. You should copy app/code/core/mage/Api2/Model/Resource.php to app/code/local/mage/Api2/Model/Resource.php and make your changes in he local file. You should NEVER edit the core Magento files, otherwise, if you upgrade your Magento system, you lose your changes.
We can set the default number of product response and maximum number of products.
Go to app/code/core/mage/Api2/Model/Resource.php
Change as per our need.
/**##+
* Collection page sizes
*/
const PAGE_SIZE_DEFAULT = 10;
const PAGE_SIZE_MAX = 200;
/**##-*/
Commision Junction is the name of an affiliate company. I am not familiar with SOAP, WSDL and with web services in general, but wanted to quickly test the data coming back from their affiliate api. Cannot make it work though. They provide a page for their API
I tried smtg like:
public function testCJApi() {
$url = "http://" . $this->user . ":" . $this->password . "#datatransfer.cj.com/datatransfer/files/" . $this->account . "/outgoing/commission_report.csv";
$xml = simplexml_load_file($url);
if (isset($xml)) {
return ($xml
? $this->formatJsonReturn($xml, array("txt"=>"CJ Results OK","code"=>""))
: $this->formatJsonReturn("", array("txt"=>"CJ Results Empty","code"=>""))
);
}
}
but it didn't give me any results. I just need to quickly test the data coming back.
The API link they provide is
http://api.affiliatewindow.com/v4/MerchantService?wsdl.
I have figured it out myself:
public function testCJApi() {
$uri = "https://commission-detail.api.cj.com/v3/commissions?date-type=posting&start-date=2013-02-15&end-date=2013-02-17"; // can be other api uri, this is one of them
$context = stream_context_create(
array(
'http' => array(
'method' => 'GET',
'header' => 'Authorization: ' . 'YOUR API KEY GOES HERE'
)
)
);
$x = file_get_contents($uri, false, $context);
$response = new SimpleXMLElement($x);
return $response->asXML();
}