I'm trying to get the latest changes from my documentlist in Sharepoint using PHP and GetListItemChangesSinceToken. I'm using phpSPO as a SDK since there aren't any official Sharepoint SDK's for PHP.
So far I have this:
$payload = array(
'query' => array(
'__metadata' => array('type' => 'SP.ChangeLogItemQuery'),
'ViewName' => '',
'QueryOptions'=> '<QueryOptions><Folder>Shared Documents</Folder></QueryOptions>'
)
);
$headers = array();
$headers["X-HTTP-Method"] = "MERGE";
$changes = $this->request->executeQueryDirect($this->settings->URL . "/_api/web/Lists/GetByTitle('Documents')/GetListItemChangesSinceToken", $headers, $payload);
Which returns: {"error":{"code":"-2147467261, System.ArgumentNullException","message":{"lang":"en-US","value":"Value cannot be null.\r\nParameter name: query"}}}
I've tried changing the X-HTTP-Method and changing the array to fit the documented JSON/XML request (XML in JSON objects, come on Microsoft)
First approach
The following example demonstrates how to utilize GetListItemChangesSinceToken method:
$listTitle = "Documents";
$payload = array(
'query' => array(
'__metadata' => array('type' => 'SP.ChangeLogItemQuery'),
'ViewName' => '',
'QueryOptions'=> '<QueryOptions><Folder>Shared Documents</Folder></QueryOptions>'
)
);
$request = new ClientRequest($webUrl,$authCtx);
$options = array(
'url' => $webUrl . "/_api/web/Lists/GetByTitle('$listTitle')/GetListItemChangesSinceToken",
'data' => json_encode($payload),
'method' => 'POST'
);
$response = $request->executeQueryDirect($options);
//process results
$xml = simplexml_load_string($response);
$xml->registerXPathNamespace('z', '#RowsetSchema');
$rows = $xml->xpath("//z:row");
foreach($rows as $row) {
print (string)$row->attributes()["ows_FileLeafRef"] . "\n";
}
Second approach
Since SharePoint REST Client SDK for PHP now supports GetListItemChangesSinceToken method, the previous example could be invoked like this:
$list = $ctx->getWeb()->getLists()->getByTitle($listTitle);
$query = new ChangeLogItemQuery();
//to request all the items set ChangeToken property to null
$query->ChangeToken = "1;3;e49a3225-13f6-47d4-a146-30d9caa05362;635969955256400000;10637059";
$items = $list->getListItemChangesSinceToken($query);
$ctx->executeQuery();
foreach ($items->getData() as $item) {
print "[List Item] $item->Title\r\n";
}
More examples could be found here under phpSPO repository.
Related
I am working on a php application that requires me to interact with each merchants store using the WooCommerce Rest API and i am trying to auto-generate the rest api keys like it was documented on their documentation but my callback_url endpoint won't fire and i don't receive the auto-generated keys sent to the callback endpoint.
Here is my code to Create an authentication endpoint URL
public function integrate()
{
$url = $this->input->post('url');
$title = $this->input->post('title');
$user_id = $this->session->userdata('client_id');
$save = $this->Store_model->save_store($user_id,$url, $title);
$genKeyEndpoint = '/wc-auth/v1/authorize';
$params = [
'app_name' => 'App Name',
'scope' => 'read_write',
'user_id' => $user_id,
'return_url' => base_url('stores/integrateForm'),
'callback_url' => base_url('stores/callback-endpoint')
];
$query_string = http_build_query( $params, null, '&', PHP_QUERY_RFC3986 );
$wooAuth = $url . $genKeyEndpoint . '?' . $query_string;
redirect($wooAuth);
}
and here is my code to retrieve the generated keys and store in my database
public function save_api_key() {
$post_data = json_decode(file_get_contents('php://input'), true);
$wooResponseData = [
'consumer_key' => $post_data['consumer_key'],
'consumer_secret' => $post_data['consumer_secret']
];
$this->Store_model->updateStoreKeys($this->session->userdata('client_id'), $wooResponseData);
}
My application is running on codeigniter.
In my wp project, I am using Assently for e-signature implementation. Though I have an account and created a pdf form file to be filled by the user I just am not able to proceed a bit. I am finding documentation not clear.
Also, I am not clear about what needs to be done so that the user will be shown form to process the transaction.
So, any help/suggestions to proceed forward is appreciated.
I have tried the following based on assently-laravel. But it's asking me to login. What is an error here?
Code:
define('ASSENTLY_DEBUG', true);
define('ASSENTLY_KEY', 'key');
define('ASSENTLY_SECRET', 'secret-generated');
include_once('assently/Assently.php');
$assently = new Assently();
$assently->authenticate(ASSENTLY_KEY, ASSENTLY_SECRET);
$url = 'https://test.assently.com/api/v2/createcasefromtemplate';
$default = array(
'Id' => '5a0e0869-' . rand(1111, 9999) . '-4b79-' . rand(1111, 9999) . '-466ea5cca5ce'
);
$data = array(
'auth' => $assently->auth(),
'templateId' => '0e004e2b-b192-4ce2-8f47-d7a4576d7df6',
'newCaseId' => '5a0e0869-' . rand(1111, 9999) . '-4b79-' . rand(1111, 9999) . '-466ea5cca5ce',
'agentUsername' => ''
);
$data = array(
'json' => $data
);
$options = array(
'http' => array(
'header' => "Content-type: application/json; charset=utf-8\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo '<pre>';
print_r($result);
die;
create this class inside assently folder
use Assently\AssentlyCase;
use Exception;
class CustomAssentlyCase extends AssentlyCase
{
public function createFromTemplate($data)
{
$default = [
'newCaseId' => '5a0e0869-'.rand(1111, 9999).'-4b79-'.rand(1111, 9999).'-466ea5cca5ce'
];
$json = array_merge($default, $data);
try{
$response = $this->client->post($this->url('createcasefromtemplate'), [
'auth' => $this->assently->auth(),
'json' => $json
]);
}catch(Exception $e){
print_r($e->getMessage());
}
return $response;
}
}
Use
define('ASSENTLY_DEBUG', true);
define('ASSENTLY_KEY', 'key');
define('ASSENTLY_SECRET', 'secret-generated');
include_once('assently/Assently.php');
include_once('assently/CustomAssentlyCase.php');
$assently = new Assently();
$assently->authenticate(ASSENTLY_KEY, ASSENTLY_SECRET);
$data = array(
'templateId' => '0e004e2b-b192-4ce2-8f47-d7a4576d7df6',
'newCaseId' => '5a0e0869-'.rand(1111, 9999).'-4b79-'.rand(1111, 9999).'-466ea5cca5ce',
'agentUsername' => 'agentUsername' // PUT your agent username here it is required
);
$customAssentlyCase = new CustomAssentlyCase($assently);
$result = $customAssentlyCase->createFromTemplate($data);
print_r($result);
Try this, though not tested but should work. Good luck.
I work at an MSP and I'm trying to integrate our ticketing system (Autotask) with Slack. So far, I have set up an HTTP POST request to some PHP code when a new ticket is created, which posts a chat message in a certain channel ("New Ticket Created! [Ticket Title] - [Ticket Description]" etc.) using Incoming Webhooks. Below is the important bits of that code:
<?php
require_once __DIR__ . '/src/autoload.php';
//Gets ticket number from HTTP POST
$ticketNum = $_POST['subject'];
//Generated at api.slack.com/apps > Incoming webhooks (for the channel I'm posting in)
$responseURL = 'https://hooks.slack.com/services/XXXXXXXXXXXX';
class Response {
public $response_type = "";
public $text = "";
public $attachments = array();
}
//*****
//QUERYING TICKETING SYSTEM API FOR TICKET INFORMATION
//THIS CODE WORKS, OMITTED FOR EASIER READING
//*****
$r = new Response();
$r->response_type = "in_channel";
$r->text = "New Ticket Created: \n*".$ticketName."* (".$ticketNum.") \n".$ticketDescription;
$r->attachments = array(
0 => array('callback_id' => 'newTicketAction', 'text' => 'Select an action:',
'color' => '95baa9', 'actions' => array(
0 => array('name' => 'newTicketAction', 'text' => 'Accept', 'type' => 'button',
'value' => 'accept', 'style' => 'primary'),
1 => array('name' => 'newTicketAction', 'text' => 'View', 'type' => 'button',
'value' => 'view')
)
)
);
header('Content-Type: application/json');
//Using CURL
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $responseURL,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => json_encode($r)
));
$resp = curl_exec($curl);
var_dump($resp);
curl_close($curl);
?>
This works and posts a message in the correct channel that looks like this:
The problem I'm having is with responding to those button clicks. I have my Request URL set up, pointing to another PHP script. My understanding is that the button click sends another HTTP POST request to this page with a JSON object, which has a payload item. My hunch is I'm accessing this incorrectly. My code is below:
<?php
require_once __DIR__ . '/src/autoload.php';
//get information from HTTP POST, sent at button click
$payload = $_POST['payload'];
$data = json_decode($payload,true);
$responseURL = $data -> response_url;
class Response {
public $response_type = "";
public $text = "";
public $attachments = array();
}
$r = new Response();
$r->response_type = "in_channel";
$r->text = "Ticket Accepted";
header('Content-Type: application/json');
//Using CURL
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $responseURL,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => json_encode($r)
));
$resp = curl_exec($curl);
var_dump($resp);
curl_close($curl);
?>
You can see right now I'm just trying a simple response message for testing purposes, but when I click the 'Accept' button I get the following response:
I've scoured the web and cannot find a solid tutorial on Interactive Messages which includes "receiving" button clicks. Any help would be greatly appreciated.
PHP syntax was wrong for accessing array element.
I am trying to get search results as per the api documentation
Here is what I wrote in PHP
require_once 'HTTP/Request2.php';
$api_key = 'my_bing_search_api_key';
$url_encoded_keyword = urlencode('animation concepts and tutorials');
$request = new \Http_Request2('https://api.cognitive.microsoft.com/bing/v5.0/search');
$headers = [
'Ocp-Apim-Subscription-Key' => $api_key
];
$request->setHeader($headers);
$url = $request->getUrl();
$parameters = [
'q' => $url_encoded_keyword,
'count' => '10',
'offset' => '0',
'safesearch' => 'Strict',
);
$url->setQueryVariables($parameters);
$request->setMethod(\HTTP_Request2::METHOD_GET);
$request->setBody("{body}");
$search_result = null;
try {
$response = $request->send();
$search_results = json_decode($response->getBody(), true);
return $search_results;
} catch (HttpException $ex) {
return [];
}
I am getting response but it is not having webPages property. It has _type, rankingResponse, relatedSearches and videos properties only.
I tested the same request in the api console. There I am getting the webPages property in the json response.
Any ideas what could have been the reason why I am not getting the webPages in PHP but works on microsoft's api tester site?
From the code snippet, you are passing the keyword to Bing web search API after encoding it.
$url_encoded_keyword = urlencode('animation concepts and tutorials');
$parameters = [
'q' => $url_encoded_keyword,
'count' => '10',
'offset' => '0',
'safesearch' => 'Strict',
);
Try without encoding the keyword. From their API testing console, HTTP request for the same keyword would appear as
https://api.cognitive.microsoft.com/bing/v5.0/search?q=animation
concepts and tutorials&count=10&offset=0&mkt=en-us&safesearch=Moderate
I'm trying to access a webservice. I'm already using this webservice from an android app but now I need to access some functions from a php document. If I use chromes advanced rest client application for testing it works fine if I select the POST option and application/x-www-form-urlencode as content-type. But when I try to access the webservice from my PHP file I get the response from the server that it can't find the value "tag". This is the code:
$data = array( 'tag' => 'something');
$options = array('http' => array(
'method' => 'POST',
'content' => $data,
'header' => "Content-Type: application/x-www-form-urlencode")
);
$context = stream_context_create($options);
$url = 'myurl';
$result = file_get_contents($url,false,$context);
$response = json_decode($result);
What is wrong with this code?
Thanks for any help!
Try this:
$data = http_build_query( array( 'tag' => 'something') );
As defined here, "Content" value must be a string: http_build_query generate the URL-encoded query string you need.