JSON respone from CURL request from HTTP API - HTML transform - api

I am able to send the CURL request to a test http API and getting the response as well.
My CURL request is like:
$request = "";
$param['auth-userid'] = '449735';
$param['api-key'] = 'apikey';
$param['domain-name'] ='sambalpurodisha';
$param['tlds']='com';
foreach ($param as $key => $val) {$request.= $key ."=".urlencode($val);
$request .="&";}
$request=substr($request,0,strlen($request)- 1 );
$url = "https://test.httpapi.com/api/domains/available.json?".$request;
$ch = curl_init($url);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec( $ch );
curl_close( $ch );
I am getting a response like:
{"sambalpurodisha.com":{"status":"available","classkey":"domcno"}}
Now I have tried everything to format the result in more readable manner like
Domain - sambalpurodisha.com is available for purchase.
I have tried number of suggestions here at stack, but none worked for me. A direction where I can look for

You'll need to decode the returned JSON string as either a JSON object or an associative array, then iterate through it:
$rawJSON = '{"sambalpurodisha.com":{"status":"available","classkey":"domcno"}}';
$jsonArray = json_decode($rawJSON, true);
foreach($jsonArray as $key => $val) {
echo 'Domain: ' . $key . ' status: ' . $val['status'] . "\n";
}

Related

Could not find RETS data in TREB VOW Data Access

This is connection file
date_default_timezone_set("Canada/Eastern");
require_once("vendor/autoload.php");
$config = new \PHRETS\Configuration;
$config->setLoginUrl('http://retsau.torontomls.net:6103/rets-treb3pv/server/login')
->setUsername('XXXX1')
->setPassword('76XXXXX')
->setRetsVersion('1.7');
$rets = new \PHRETS\Session($config);
$connect = $rets->Login();
echo '<pre>';
var_dump($connect);
echo '</pre>';
if($connect)
{ echo "connetced";
else{
echo 'not connected';
}
After connection could not find any data
$results = $rets->Search('Property', 'A', '*', ['Limit' => 3, 'Select' => 'LIST_1,LIST_105,LIST_15,LIST_22,LIST_87,LIST_133,LIST_134']);
foreach ($results as $r) {
echo '<pre>';
var_dump($results);
echo '</pre>';
}
I need to display this data after connection
First you should check TREB metadata, find correct field names and use them in the search. Moreover, you should send a query as 3rd argument of Search function.

remove alias using google php api

Is it possible to remove a user's alias using a service account and the google php api?
I have tried several methods, including:
$optParams = array('alias'=>$userMail);
$remove_alias = $directory_service->users->delete($user_info['primaryEmail'],$optParams);
$remove1 = $directory_service->getAuth()->authenticatedRequest($remove_alias);
AND
$url = "https://www.googleapis.com/admin/directory/v1/users/" . $user_info['primaryEmail'] . "/aliases/" . $userMail;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$request_headers = array();
$request_headers['Content-type'] = 'application/json';
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$output = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($output, 0, $header_size);
$body = substr($output, $header_size);
curl_close($ch);
I get either error 401, Login Required. Or error, unidentified 'alias'
It looks like calling users->delete vs users_aliases->delete might be causing the issue. Maybe something like this would work?
require_once $CLIENT_API_PATH . '/vendor/autoload.php';
date_default_timezone_set('America/Los_Angeles');
// this is the service account json file used to make api calls within the domain
$serviceAccount = $JSON_PATH . '/service-account-creds.json';
// delegate the work to an account with ability to make changes
$impersonateUser = 'adminWithRolesToMakeChanges#your.domain.com';
// these are the scope(s) used.
define('SCOPES', implode(' ', array( Google_Service_Directory::ADMIN_DIRECTORY_USER_ALIAS ) ));
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $serviceAccount );
$client = new Google_Client();
// loads the json service account file.
$client->useApplicationDefaultCredentials();
$client->setScopes(SCOPES);
$client->setSubject($impersonateUser);
$dirObj = new Google_Service_Directory($client);
// This is the user account to delete the alias from
$userKey = 'joeschmo#your.domain.com';
$alias = 'alias2delete#your.domain.com';
$aliasObj = new Google_Service_Directory_Alias( array( 'alias' => $alias ) );
// delete the alias from the account.
$results = $dirObj->users_aliases->delete($userKey, $aliasObj);
// If successful, this method returns an empty response body.
print_r($results);
See Also:
https://developers.google.com/admin-sdk/directory/v1/reference/users/aliases/delete

Shopify API getting order by name or order_number

Im using a plugin for CakePHP to make the calls to obtain certain orders. I can call all orders with certain fields, but I was wondering how would I have to make the call to get the orders with a certain name or order_number? Here is the source for the call to Shopify. Its already authenticated and everything:
public function call($method, $path, $params=array())
{
if (!$this->isAuthorized())
return;
$password = $this->is_private_app ? $this->secret : md5($this->secret.$this->ShopifyAuth->token);
$baseurl = "https://{$this->api_key}:$password#{$this->ShopifyAuth->shop_domain}/";
$url = $baseurl.ltrim($path, '/');
$query = in_array($method, array('GET','DELETE')) ? $params : array();
$payload = in_array($method, array('POST','PUT')) ? stripslashes(json_encode($params)) : array();
$request_headers = in_array($method, array('POST','PUT')) ? array("Content-Type: application/json; charset=utf-8", 'Expect:') : array();
$request_headers[] = 'X-Shopify-Access-Token: ' . $this->ShopifyAuth->token;
list($response_body, $response_headers) = $this->Curl->HttpRequest($method, $url, $query, $payload, $request_headers);
$this->last_response_headers = $response_headers;
$response = json_decode($response_body, true);
if (isset($response['errors']) or ($this->last_response_headers['http_status_code'] >= 400))
throw new ShopifyApiException($method, $path, $params, $this->last_response_headers, $response);
return (is_array($response) and (count($response) > 0)) ? array_shift($response) : $response;
}
private function shopApiCallLimitParam($index)
{
if ($this->last_response_headers == null)
{
return 0;
}
$params = explode('/', $this->last_response_headers['http_x_shopify_shop_api_call_limit']);
return (int) $params[$index];
}
...and the code that makes the GET call:
// I only want the id and title of the collections
$fields = "fields=name,id,status,financial_status,fulfillment_status,billing_address,customer";
// get list of collections
$custom_collections = $this->ShopifyAPI->call('GET', "/admin/orders.json", $fields);
$this->set('collections', $custom_collections);
I think I'm missing the place where I can put the conditions for the call to get certain orders. I've already read the API documentation but can't seem to get the answer.
I've tried putting the ?name=%231001 on the url after .json to try and get the order #1001, but it brings back a empty array.
Then I tried ?order_number=1001 but it brings me every order with as well 1001 D: This is really confusing, Could anyone help me?
Thanks in advance.
Well I found out that you can actually get the order using the name or order_number. Its another property that is not listed on the documentation for some reason. But in the URL, if your using another language, all you have to add in the GET is admin/order.json?name=%2310001&status=any this is to get the order 10001 so just add the order_number after the %23. I saw this on a forum in Shopify university, I was just implementing this wrong on my code. If your using the CakePhp shopify plugin like me all I did was add on the $field the ?name=%23". number ."&status=any";
Ill leave the code here:
$this->layout = 'main';
$order_number = "18253";
$fields = "name=%23". $order_number ."&status=any";
$order = $this->ShopifyAPI->call('GET', "/admin/orders.json", $fields);
if (!empty($order)) {
$this->set('order', $order);
} else {
$this->Session->setFlash('<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button> No existe el numero de orden ingresado.','default',array('class' => 'alert alert-danger alert-dismissible', 'type' => 'alert'));
}
Hope this helps someone :P

Cloadflare Return 'rec_id' For 1 'A' Record in a Zone : PHP

I have a PHP script that adds a new 'A' record to a Cloudflare zone, however, by default these new 'A' records are set as non-active by Cloudflare and now days you can not set them as active when creating them.
So, to edit the new record to set it as active, you need the 'A' records 'rec_id'. In this case action 'rec_load_all' can't be used as there are too many zone 'A' records and I don't think you can filter the request (could be wrong & would be good to be wrong). The zone needs to be filtered.
I have tried the following 'dns_get_rec_one' but it just returns 'NULL' with no error message:
function returnId(){
$request = array();
$request['a'] = 'dns_get_rec_one';
$request['tkn'] = $this->tkn;
$request['email'] = $this->apiEmail;
$request['z'] = 'domain.com';
$request['name'] = 'sub.domain.com';
$response = #json_decode(file_get_contents('https://www.cloudflare.com/api_json.html?' . http_build_query($request)), true);
}
Any ideas as I have little experience with API interactions?
Thanks
Ok, I have worked this out with some help.
When you make the CURL 'rec_new' call to Cloudflare the response includes the 'rec_id' for the new "A" record. This can then be used as the 'id' in the next CURL 'rec_edit' call to edit the record as being active.
The guys at Cloudflare support answer within 24hrs as well and are helpful.
Snippets from class bellow:
private function newSub(){
$fields = array(
'a' => 'rec_new',
'tkn' => $this->tkn,
'email' => $this->apiEmail,
'z' => $this->domain,
'type' => 'A',
'name' => $this->subName,
'content' => $this->content,
'ttl' => 1
);
//url-ify the data for the POST
foreach($fields as $key=>$value){
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, 'https://www.cloudflare.com/api_json.html');
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
$response = curl_exec($ch);
//close connection
curl_close($ch);
$response = json_decode($response,true);
if(!$response || $response['result'] != 'success'){
$responseError = $response['msg'];
// ERROR Handling
}else{
// Set rec_id for from the nw A record
$this->rec_id = $response['response']['rec']['obj']['rec_id'];
// Activate
$this->makeActive();
}
}
private function makeActive(){
$request['a'] = 'rec_edit';
$request['tkn'] = $this->tkn;
$request['email'] = $this->apiEmail;
$request['z'] = $this->domain;
$request['id'] = $this->rec_id;
$request['type'] = 'A';
$request['name'] = $this->subName;
$request['content'] = $this->content;
$request['service_mode'] = '1';// Make active
$request['ttl'] = '1';
$response = #json_decode(file_get_contents('https://www.cloudflare.com/api_json.html?' . http_build_query($request)), true);
//var_dump($response); die;
if(!$response || $response['result'] != 'success'){
$responseError = $response['msg'];
// ERROR Handling
}
}
Hope that this helps someone.

Create an video gallery with UStream API

I'm trying to create a simple video gallery that pulls the videos from the recorded shows of a specific Ustream user. I've been doing some research and looking at the API Documentation but can't seem to figure it out.
This is what I have so far.
$request = 'http://api.ustream.tv';
$format = 'php'; // this can be xml, json, html, or php
$args .= 'subject=[channel]';
$args .= '&uid= [the-crossfader-show]';
$args .= '&command=[getCusomEmbedTag]';
$args .= '&params=[autoplay:false; mute:false; height:100; width:100;]';
$args .= '&page=[1]';
$args .= '&limit=[20]';
$args .= '&key=[4872929558631FEB4E9AEE8DDF080F28]';
// Get and config the curl session object
$session = curl_init($request.'/'.$format.'?'.$args);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
//execute the request and close
$response = curl_exec($session);
curl_close($session);
// this line works because we requested $format='php' and not some other output format
$resultsArray = unserialize($response);
// this is your data returned; you could do something more useful here than just echo it
print_r $resultsArray['result'];
I'm not really sure what to do after this to turn it into a simple gallery. Anyone have any experience with the UStream API or have suggestions on what to do next?
I know this is an old post, but I have been looking into the API myself for the first time today and came across this. Try using arguments without the square brackets around your values? example:
$args .= 'subject=channel';
$args .= '&uid=the-crossfader-show';
$args .= '&command=getCusomEmbedTag';
$args .= '&params=autoplay:false; mute:false; height:100; width:100;';
$args .= '&page=1';
$args .= '&limit=20';
$args .= '&key=4872929558631FEB4E9AEE8DDF080F28';
You also had an extra space before the-crossfader-show. I haven't tried this code, but perhaps this will help. Also, setting subject=user would get you all the videos for a user, vs subject=channel which i believe to only be a single video stream?