Moodle api list all cohorts - api

Im looking to get all cohorts, i can get the info if i have the id but i want all the cohorts names and ids. Please let me know if theres a way to do this. Thanks in advance
$functionname = 'core_cohort_get_cohorts';
$cohortIDS = array( '1' );
$data_string = http_build_query(array('cohortids' => $cohortIDS));
$utoken = 'mytoken';
$adduser = 'yoururl/webservice/rest/server.php? wstoken='.$utoken.'&wsfunction='.$functionname.'&moodlewsrestformat=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$adduser);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
echo '<pre>';
print_r(json_decode($server_output));
echo '</pre>';

You could add more method to core_cohort to get all cohort as the following:
In the cohort/externallib.php, add more method:
public static function get_all_cohorts(){
global $DB;
$cohortids = $DB->get_records('cohort', null, null, 'id');
$arrids = array();
foreach($cohortids as $id){
$arrids[] = $id->id;
}
return (new core_cohort_external())->get_cohorts($arrids);
}
In addition, you have to register this webservice called 'core_cohort_get_all_cohorts' to call this webservice from outside. Please tell me if you have further question.

Related

Whatsapp Cloud Api get Media Object ID

I want to upload the media via Whatsapp cloud API in order to send the media to WhatsApp, I have followed the link below, but having the stated issue.
https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#get-media-id
Array ( [error] => Array ( [message] => (#100) The parameter messaging_product is required. [type] => OAuthException [code] => 100 [fbtrace_id] => AQPftb9Bf343375gQ_QytxNen ) ) 400
My Code is
// #1640191015925.jpg on my root folder
$post = [
'file' => '#1640191015925.jpg;type=image/jpeg',
'messaging_product' => 'whatsapp'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v13.0/Phone-Number-ID/media');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$headers = array();
$YOUR_WHATSAPP_ACCESS_TOKEN = '+++++++';
$headers[] = "Authorization: Bearer $YOUR_WHATSAPP_ACCESS_TOKEN";
$headers[] = "Content-Type: image/jpeg";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = json_decode(curl_exec($ch), true);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//$MEDIA_OBJECT_ID = $result['media'][0]['id']; //MEDIA OBJECT ID
print_r($result);
print_r($httpcode);
?> ```
I had the same issue, after some digging around I wrote this function to upload a file and return the id.
function waFileUploader($token, $file, $senderid){
$mime = mime_content_type($file);
$info = pathinfo($file);
$name = $info['basename'];
$curlfile = new CURLFile($file, $mime, $name);
$filedata = array("messaging_product"=>"whatsapp", "file" => $curlfile);
$url = 'https://graph.facebook.com/v13.0/' . $senderid . '/media';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Authorization: Bearer $token", "Content-Type: multipart/form-data"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $filedata);
$r = curl_exec($ch);
curl_close($ch);
$id = json_decode($r, true);
return $id["id"];
}

Instagram basic display api withlaravel

I'm working on Instagram basic api, in my project user can connect their Instagram feed to our project. For that I'm generating a short live authentication code but could not get the long live access code. Its returning null.
Here is my code for getting long live access code.
$client_secret = "###############";
$client_id = "###############";
$redirect_uri = '#########';
$short_access_token = "####"; // getting this from another function
$ig_rtu = 'https://api.instagram.com/oauth/access_token?client_id='.$client_id.'&client_secret='.$client_secret.'&grant_type=authorization_code&redirect_uri='.$redirect_uri.'&access_token='.$short_access_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ig_rtu);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$ig_new = curl_exec($ch);
curl_close ($ch);
return $ig_new = json_decode($ig_new, true);
In postman this works fine but I could not get it done in Laravel.
What I'm missing here ?
Any lead is appreciated.
I've done this in 3 steps. I will leave the code out for step 1 and include my implementation for step 2 and 3.
Steps:
get the code
get the short lived token using the code
get the long lived token using the short lived token
Here are the public variables used.
public $appId = null;
public $appSecret = null;
public $redirectUrl = null;
// Instagram request data
public $socialUserId = null;
public $socialAuthCode = null;
public $socialUserName = null;
public $socialShortAccessToken = null; // 1 hour
public $socialLongAccessToken = null; // 60 days
public $socialLongAccessTokenExpiresInSeconds = null; // 5183910 seconds || 59.99896 days - relative to date created
Step 2:
public function getUserIdAndShortAccessToken(){
/* Get UserId and access Token
https://api.instagram.com/oauth/access_token \
-F client_id={app-id} \
-F client_secret={app-secret} \
-F grant_type=authorization_code \
-F redirect_uri={redirect-uri} \
-F code={code}
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
'https://api.instagram.com/oauth/access_token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$post = array(
'client_id' => $this->appId,
'client_secret' => $this->appSecret,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirectUrl,
'code' => $this->socialAuthCode,
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$curlResult = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
curl_close($ch);
return;
}else{
curl_close($ch);
}
$this->socialUserId = json_decode($curlResult)->user_id;
$this->socialShortAccessToken = json_decode($curlResult)->access_token;
}
Step 3:
public function getLongAccessToken(){
/* Get long-lived token
curl -i -X GET "https://graph.instagram.com/access_token
?grant_type=ig_exchange_token
&client_secret={instagram-app-secret}
&access_token={short-lived-access-token}"
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.instagram.com/access_token?grant_type=ig_exchange_token&client_secret='
.$this->appSecret.'&access_token='
.$this->socialShortAccessToken
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curlResult = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
curl_close($ch);
return;
}else{
curl_close($ch);
}
$this->socialLongAccessToken = json_decode($curlResult)->access_token;
$this->socialLongAccessTokenExpiresInSecond = json_decode($curlResult)->expires_in;
}

How to create a table from this ForEach Loop?

I am trying to fetch all the domains I have purchased from Godaddy account using their API. However, It's not showing in a table format for proper view. Can anybody guide me on how to create a table out of this API results? Currently, it's showing results each at a different line but I prefer to view them in a table format.
Thanks!!
<?php
$domains = getDomains();
// check if error code
if(isset($domains['code'])){
$msg = explode(":",$domains['message']);
$msg = '<h2 style="text-align:center;">'.$msg[0].'</h2>';
// proceed if no error
} else {
$msg = '';
foreach ($domains as $domain){
$msg .= $domain['domain'].'</br>';
$msg .= $domain['createdAt'].'<br>';
$domain['createdAt'];
$domain['domain'];
$domain['domainId'];
$domain['expirationProtected'];
$domain['expires'];
$domain['holdRegistrar'];
$domain['locked'];
$domain['nameServers'];
$domain['privacy'];
$domain['renewAuto'];
$domain['renewDeadline'];
$domain['renewable'];
$domain['status'];
$domain['transferProtected'];
}
}
echo $msg;
function getDomains(){
$status = 'ACTIVE';
$limit = '999';
$url = "https://api.godaddy.com/v1/domains?statuses=$status&limit=$limit";
// set your key and secret
$header = array(
'Authorization: #########'
);
//open connection
$ch = curl_init();
$timeout=60;
//set the url and other options for curl
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); // Values: GET, POST, PUT, DELETE, PATCH, UPDATE
//curl_setopt($ch, CURLOPT_POSTFIELDS, $variable);
//curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
//execute call and return response data.
$result = curl_exec($ch);
//close curl connection
curl_close($ch);
// decode the json response
$dn = json_decode($result, true);
return $dn;
}
?>

Zoho crm API sometimes returns error

I'm trying to searchRecord using the Zoho CRM API and I sometimes get the following error:
Array ( [response] => Array ( [error] => Array ( [message] => Unable
to process your request. Please verify whether you have entered proper
method name,parameter and parameter values. [code] => 4600 ) [uri] =>
/crm/private/json/Contacts/searchRecords ) )
My problem is that sometimes everything works fine and sometimes I get this error
define("TARGETURL", "https://crm.zoho.com/crm/private/json/Contacts/searchRecords");
$parameter = array(
'scope' => 'crmapi',
'authtoken' => AUTHTOKEN,
'selectColumns' => 'All',
'criteria' => '(Account Name:'.$accountName.')',
'fromIndex' => $fromIndex,
'toIndex' => $toIndex
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, TARGETURL);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameter);
$result = curl_exec($ch);
curl_close($ch);
Try search field with double quotation mark, following is script to search email from lead module.
$xmlData = "(Email:$email)";
$ch = curl_init('https://crm.zoho.com/crm/private/xml/Leads/searchRecords');
curl_setopt($ch, CURLOPT_VERBOSE, 1); //standard i/o streams
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Turn off the server and peer verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set to return data to string ($response)
curl_setopt($ch, CURLOPT_POST, 1); //Regular post
$authtoken = "********************************";
$query = "authtoken=" . $authtoken . "&scope=crmapi&criteria=" . $xmlData;
curl_setopt($ch, CURLOPT_POSTFIELDS, $query); // Set the request as a POST FIELD for curl.
$response = curl_exec($ch);
curl_close($ch);

Facebook API call- "This API version is deprecated"

I'm trying to call to Photo.upload on the the Facebook API server. As far as I can tell the code to construct the call is good but I can't understand the responce I'm getting back from the server. As far as I can see, this call is ment to work and other people don't get this problem. I can only reason that something is wrong with the code. The commented stuff is an artifact of me trying different things to get a different responce from the server. The original code that I've changed was in part taken from an example of how to do this which I couldn't get to work either really:
http://www.jaisenmathai.com/blog/2008/11/27/using-the-facebook-api-to-upload-photos/
Server Responce:
12 This API version is deprecated method photos.upload api_key b92cee19a33c861275bfce4695896e44 call_id 1250194789.61 garden_jpg /var/www/vivaladan/pictureyourselfhull/garden.jpg v 0 sig 896ee95339cad24ce7e64a05ca764123
Code:
$key = b92cee19a33c861275bfce4695896e44;
$ver = 1.0;
$cid = microtime(true);
$uid = BIGINT;
$file= "garden.jpg";
$args = array(
//amethod => photos.upload,
v => $ver,
api_key => $key,
//uid => $uid,
call_id => $cid,
//format => XML
);
$args[basename($file)] = realpath($file);
signRequest($args,$sec);
$postString = "";
foreach($args as $index => $value) {
$postString .= $index ."=".$value."&";
}
$postString = trim($postString, '&');
$ch = curl_init();
$url = "http://api.facebook.com/restserver.php?method=photos.upload";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
$data = curl_exec($ch);
echo $data;
function signRequest(&$args, $secret){
ksort($args);
$sig = "";
foreach($args as $k => $v){
$sig .= $k . '=' . $v;
}
$sig .= $secret;
$args[sig] = md5($sig);
}
Rest API call is not working anymore.
Try GraphApi
I'm guessing it's because you're using $ver = 0.0; - there's no 0.0 version of the API.
try using api.new.facebook.com API URL instead