I have bought one android application with web service made in codeigniter. There API in this web service is like below.
<?php
if (!defined("BASEPATH"))
exit("No direct script access allowed");
class Site extends back {
public function __construct() {
parent::__construct();
$this->lang->load("site", "english");
$this->load->model("site_model", "site");
}
//=====================================================================
public function get_updates($last_author, $last_quote) {
$this->db->where("_auid > ", $last_author);
$this->db->where("au_status", 1);
$result["authors"] = $this->db->get("authors")->result_array();
$this->db->select("quotes.*, authors._auid, authors.au_status");
$this->db->from("quotes");
$this->db->join("authors", "authors._auid = quotes.qu_author AND authors.au_status = 1");
$this->db->where("_quid > ", $last_quote);
$this->db->where("qu_status", 1);
$result["quotes"] = $this->db->get()->result_array();
echo json_encode($result);
}
}
I am learning php yet. I have made another fresh corephp web service for use. I am not understanding above api and so not able to make similar api for my new web service. Both web service use same database...anyone can please suggest me how can I use above API in my new corephp web service ?
sorry for my bad knowledge.
Thanks
It is very simple
function get_updates get 2 parameter as input
1) $last_author //value of _auid(id) field belong to table author
2) $last_quote //value of _quid(id) field belong to table quotes
$this->db->where("_auid > ", $last_author);
$this->db->where("au_status", 1);
$result["authors"] = $this->db->get("authors")->result_array();
These lines fetch data from table author with matches value of $last_author parameter
And second query fetch data from table quotes and it is join with author with matches value of $last_quote parameter.
Join is use for smashing two or more tables into a single table.
$last_author and $last_quote is request parameter send from application.
Desired result will be stored in $result and return with json object as response data to application.
Related
Background and Context
I am attempting to create a custom REST module in SugarCRM 8.0 that joins two modules on a linked field. The two modules are;
Accounts ( a OOTB module )
ConsumerLeads ( a custom module )
These two modules are a linked field named w002_consumerleads_accounts_1 such that when I invoke the following endpoint I get a JSON response with all the ConsumerLead data associated with that specific account uuid.
/rest/v11_4/Accounts/{ account_uuid }/link/w002_consumerleads_accounts_1
However, instead of returning the joined results based on the account uuid, my custom module returns the the joined results based on a different account attribute that uniquely identifies an account.
/rest/v11_4/customer/{ customer_id }/leads
The reason I am attempting to build a custom module for this ( instead of using the predefined rest api listed above ) is due to the fact that the system invoking this call does not know the sugar account uuid, and instead knows a business key that uniquely identifies the account ( and is an attribute on the Accounts module ).
The Issue
When I use SugarQuery in my custom rest module to join Accounts and ConsumerLeads my results do not match the results specified in the first rest api ( the predefined rest api ). The result only returns the Accounts module, and does not join the ConsumerLeads module.
The issue is with the joining of the two modules on the linked field; the issue is not related to using the customer uuid v. the account uuid.
Reference Implementations
Based on the SugarCRM Developers Guide I written the following piece of code.
public function GetLinkLeads($api, $args) {
try {
$query = new SugarQuery();
$query->from(BeanFactory::getBean('Accounts'));
$query->join('w002_consumerleads_accounts_1');
$query->limit(10);
$results = $query->execute();
return $results;
} catch ( Exception $e ) {
return $e->getCode();
}
return 1;
}
Based on all the information I can gather, this function should return the first 10 account records joined with their ConsumerLeads. However, the response only contains the Accounts module data, it does not join the ConsumerLeads data.
Additionally, I have also tried the following way.
public function GetLinkLeads($api, $args) {
try {
$account = BeanFactory::getBean('Accounts', '4606e963-9213-7764-d83f-4cc050c8473d');
if ( $account->load_relationship('w002_consumerleads_accounts_1') ) {
$lead = $account->w002_consumerleads_accounts_1->getBeans();
return $lead;
}
} catch ( Exception $e ) {
return $e->getCode();
}
return 1;
}
Where 4606e963-9213-7764-d83f-4cc050c8473d is an account uuid that has associated ConsumerLeads, but I still cannot get ConsumerLeads data to return.
Question
I would like to either:
Use SugarQuery() to join these two modules based on a linked field
Use the default rest api to join the two modules based on an attribute on the Accounts module (not the account uuid)
I have found this exact question on Stack Overflow and the implementation above is based off the recommendation from that post. However, it still is not joining the records.
What you want to achieve should work OOTB using the filter API and specifying the required field's contents in your requests filter definition. Then you should receive the linked record you wanted.
It should work similar to https://stackoverflow.com/a/50608304/4068476
As quick test you could try calling this in your browser or program (here: GET, that's why I pass filter in query string)
/rest/v11_4/w002_ConsumerLeads?filter[0][w002_consumerleads_accounts_1.consumer_id]={ customer_id }
Notes:
I guessed the spelling of the module name in the URL, make sure to get uppercase/lowercase right.
If you want to receive both the Accounts and the ConsumerLeads in a single request - just use the /bulk API to wrap two requests into one.
Try the following code:
function outright_load_relationship( $left_bean, $rel_name,$return_beans = 0 ) {
$rel_obj = $left_bean->load_relationship( $rel_name );
if ( !$rel_obj ){
$rel_name =outright_get_bean_fields( $rel_name );
$rel_obj = $left_bean->load_relationship( $rel_name );
}
if( $return_beans ){
$relatedBeans = $left_bean->$rel_name->getBeans();
return $relatedBeans;
}
return $left_bean->$rel_name;
}
function outright_get_bean_fields( $left_bean, $fld_name = false, $fld_key = false ) {
if( !$fld_name && !$fld_key ) {
return $left_bean->field_defs;
} else {
foreach( $left_bean->field_defs as $key=>$value ) {
if( $fld_name && $key == $fld_name ) {
return $left_bean->field_defs[$key];
} else if( $fld_key && $value[$fld_key] == $fld_name ){
return $left_bean->field_defs[$key];
}
}
}
}
All you need to pass correct $rel_name in above functions. Please let me know if this does not work.
I have two different Facebook leads ad campaigns (A and B) connected to the same F.B page, I've connected it to a webhook following this guide using Facebook ads PHP sdk, and pass the leads to my C.R.M, everything works fine, the problem is that I can't tell if the lead came from form A or B.
I've tried to pull the from name like this:
$input = json_decode(file_get_contents('php://input'), true);
if($input)
{
$form_id = $input['entry'][0]['changes'][0]['value']['form_id'];
$form = AdsWebhookHandler::getFormName($form_id);
}
From the AdsWebhookHandlerclass:
public static function getFormName($form_id)
{
$form = new LeadgenForm($form_id);
if(!$form) return $form_id;
return $form->read();
}
But the form always returns empty ({}) for some reason.
Does anybody know how can I pull the form name? or even better - is it possible to pass custom hidden fields in the form?
Thank you all for an answer :)
O.K so I figure out how to get the form name, all I needed to do is use the getData() function included in the Facebook PHP SDK, my code looks like this now:
public function getFormName($form_id)
{
$form = new LeadgenForm($form_id,null,$this->fb_instance);
if(!$form) return $form_id;
$data = $form->read()->getData();
return isset($data['name']) ? $data['name'] : $form_id;
}
Hope it'll help someone in the future :)
Is it possible to get the asset details using asset name with Azure PHP sdk. I can get all the asset list, but it's loading first 1000 assets only.
getAssetList();
I can get single asset details using asset id.
getAsset($asset);
But in my case, I don't have asset id with me. I just have asset name alone. Now how do I get the asset details using this?
EDIT:
I got some help from Azure support saying that, we can use $skip parameter for pagination. I got code snippet in c#
for (int i = 0; i < _context.Assets.Count(); i += 1000 )
{
var assets = _context.Assets.Skip(i);
foreach (IAsset objIAsset in assets)
{
Console.WriteLine(objIAsset.Name.ToString());
}
}
How can I use this param in PHP SDK.
It seem that Azure SDK for PHP don't support skip method. However, I used the fiddler to monitor C# skip method and got the URL like this:
https://***-hs.cloudapp.net/api/Assets()?$skip=1000
So I think we can bulid up the request path like above in our PHP project and we can modify the getAssetList method in "MediaServicesRestProxy" file.
I add a function named "getAssetListBySkip($number)" into "MediaServicesRestProxy" class, the code like this:
/**
* Get asset list using skip number
*
* */
public function getAssetListBySkip($number)
{
$propertyList = $this->_getEntityList("Assets()?".'$skip='.$number);
$result = array();
foreach ($propertyList as $properties) {
$result[] = Asset::createFromOptions($properties);
}
return $result;
}
We can call this method like this:
$mediaServiceProxy = ServicesBuilder::getInstance()->createMediaServicesService(
new MediaServicesSettings("**","**/**="));
$result=$mediaServiceProxy->getAssetListBySkip(1000);
Azure Media services supports filtering by name. You can construct web request to be like
/api/assets()?$filter=Name%20eq%20'Your Name'&$top=1
You can also filter by other properties
Have you tried REST API that are used when creating, processing, managing, and delivering Assets. https://msdn.microsoft.com/en-us/library/azure/hh974277.aspx#list_an_asset but I do think we can list asset via a name directly since id is an unique indentifier of asset entity. PHP Azure SDK leverages assetId to get an Asset as well:
public function getAsset($asset)
{
$assetId = Utilities::getEntityId(
$asset,
'WindowsAzure\MediaServices\Models\Asset'
);
return Asset::createFromOptions($this->_getEntity("Assets('{$assetId}')"));
}
But in my case, I don't have asset id with me. I just have asset name
alone. Now how do I get the asset details using this?
Here are some test function code snippets for your reference:
public function testListAllAssets(){
// Setup
$asset1 = new Asset(Asset::OPTIONS_NONE);
$asset1->setName(TestResources::MEDIA_SERVICES_ASSET_NAME . $this->createSuffix());
$asset2 = new Asset(Asset::OPTIONS_NONE);
$asset2->setName(TestResources::MEDIA_SERVICES_ASSET_NAME . $this->createSuffix());
// Test
$asset1 = $this->createAsset($asset1);
$asset2 = $this->createAsset($asset2);
$result = $this->restProxy->getAssetList();
// Assert
$this->assertCount(2, $result);
$names = array(
$result[0]->getName(),
$result[1]->getName()
);
$id = array(
$result[0]->getId(),
$result[1]->getId()
);
$this->assertContains($asset1->getName(), $names);
$this->assertContains($asset2->getName(), $names);
$this->assertContains($asset1->getId(), $id);
$this->assertContains($asset2->getId(), $id);
}
public function testGetAnAssetReference(){
// Setup
$assetName = TestResources::MEDIA_SERVICES_ASSET_NAME . $this->createSuffix();
$asset = new Asset(Asset::OPTIONS_NONE);
$asset->setName($assetName);
$asset = $this->createAsset($asset);
// Test
$result = $this->restProxy->getAsset($asset);
// Assert
$this->assertEquals($asset->getId(), $result->getId());
$this->assertEquals($asset->getName(), $result->getName());
}
From: https://github.com/Azure/azure-sdk-for-php/blob/master/tests/functional/WindowsAzure/MediaServices/MediaServicesFunctionalTest.php
According to my testing, it seems that we can’t use Asset’s name to get the information of asset in Media Service.
$mediaServiceProxy = ServicesBuilder::getInstance()->createMediaServicesService(
new MediaServicesSettings("**","******"));
$asset = new Asset(Asset::OPTIONS_NONE);
$asset->setName('For-Test-wmv-Source');
//$asset don't have the value of id,
// unless execute ‘createAsset($asset)’, "$asset1" will be set the ID
$asset1 =$mediaServiceProxy->createAsset($asset);
$result2=$mediaServiceProxy->getAsset($asset1);
PHP SDK support the method named “getAsset($asset)”. Actually, the method get the Asset information by Asset id, just like the Aka's reference code.And Azure REST API don’t support the method queried by Asset’s name.
Please refer to the official document.
Alternative approach is that you can store your assets information (such as Id,URl,name and ect.) into Azure table storage when you upload them into media service. If you want to use it, you can fetch and filter the data of Asset’s name you wants from table storage.
A little more than a month ago I did a post involving the extraction the values of from a URL.
https://stackoverflow.com/questions/21231390/extracting-the-values-from-a-xml-url
It was a good answer and helped me a lot, but is there a way to generalize the code for any parameter given? The testing parameters won't be part of a formula, and this plugin I'm developing is supposed to be handled by Moodle teachers. They won't have access to my code to manually alter the parameters themselves. Here is the link.
By changing the rs parameter, I change the course. p(even/odd) and tp(semestral/anual/trimestral courses) parameters are about semesters and year is the year.
This is what I currently have.
function buildURL($year, $period, $typeperiod,$course)
{
return 'https://clip.unl.pt/sprs?lg=pt&year='.$year.'&uo=97747&srv=rsu&p='.$period.'&tp='.$typeperiod.'&md=3&rs='.$course.'&it=1030123459';
}
function doRequest_with_FileGetContents($url)
{
return download_file_content($url);
}
function processXML($xmlContent)
{
$xmlObj= new SimpleXMLElement($xmlContent);
$result=array();
foreach($xmlObj->unidade_curricular->inscritos->aluno as $aluno)
{
$result[]= $aluno->identificador;
}
return $result;
}
Testing parameters:
$year='2014';
$period='1';
$typeperiod='s';
$course='8145';
$url=buildURL($year,$period,$typeperiod,$course);
$content_b = doRequest_with_FileGetContents($url);
$dataClip = processXML($content_b);
I've tried to associate the id number of the course (course / course edit Settings/course id number) to the parameter rs, by typing:
$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
$idnumber = $course->idnumber;
and replace it in $url=buildURL($year,$period,$typeperiod,$idnumber); but the XML doesn't even process. Is this even possible to pull it off without a formal interface?
I'm trying to display the subscriber count from a MailChimp mailing list using their API, and I've got it partially working, except the code below is currently spitting out the subscriber count for all lists, rather than for one specific list. I've specified the list id in the line $listId ='XXX'; but that doesn't seem to be working. Because I have five lists in total, the output from the PHP below shows this:
10 0 0 1 9
What do I need to do in my code below to get the subscriber count from a specific list id?
<?php
/**
This Example shows how to pull the Members of a List using the MCAPI.php
class and do some basic error checking.
**/
require_once 'inc/MCAPI.class.php';
$apikey = 'XXX';
$listId = 'XXX';
$api = new MCAPI($apikey);
$retval = $api->lists();
if ($api->errorCode){
echo "Unable to load lists()!";
echo "\n\tCode=".$api->errorCode;
echo "\n\tMsg=".$api->errorMessage."\n";
} else {
foreach ($retval['data'] as $list){
echo "\t ".$list['stats']['member_count'];
}
}
?>
I just came across this function (see below) that let's me return a single list using a known list_id. The problem is, I'm not sure how to add the list_id in the function.
I'm assuming I need to define it in this line? $params["filters"] = $filters;
The MailChimp lists() method documentation can be referred to here: http://apidocs.mailchimp.com/rtfm/lists.func.php
function lists($filters=array (
), $start=0, $limit=25) {
$params = array();
$params["filters"] = $filters;
$params["start"] = $start;
$params["limit"] = $limit;
return $this->callServer("lists", $params);
}
I'd strongly recommend not mucking with the internals of the wrapper as it's not going to be nearly as helpful as the online documentation and the examples included with the wrapper. Using the wrapper means the line you tracked down will effectively be filled when make the proper call.
Anywho, this is what you want:
$filters = array('list_id'=>'XXXX');
$lists = $api->lists($filters);
Mailchimp provides a pre-built php wrapper around their api at http://apidocs.mailchimp.com/downloads/#php. This api includes a function lists() which, according to its documentation, returns among other things:
int member_count The number of active members in the given list.
It looks like this is the function which you are referring to above. All you should have to do is iterate through the lists that are returned to find the one with the proper id. From there you should be able to query the subscriber count along with a number of other statistics about the list.