Mayan EDMS API - Cannot add a document to a cabinet - maya-api

Using Mayan EDMS, I am unable to add a document to a cabinet.
I am using PHP to send, create and upload a document, which works well.
Thereafter, trying to add the document to the cabinet results in the following error
{"detail":"Not found."}
I am at a loss on how to proceed as the API documentation is not clear about the request body and fails on the execute in the swagger documentation.
// Add document to cabinet
$cabinet = $params['cabinet'];
$data = array (
'document' => [$document_id], // values must be a list []
);
$post_data = json_encode($data);
$request_url = 'http://example.com/api/v4/cabinets/' . $cabinet . '/documents/add/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$headers = [];
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: application/json';
$headers[] = 'Content-Length: ' . strlen($post_data);
$headers[] = 'Authorization: Basic YWRtaW46QndLcFNaQnJURDI3MzN5';
$result = curl_exec($ch);
// $result returns : {"detail":"Not found."}
curl_close($ch);

Related

How to PHP Convertapi HTML to PDF without physcal HTML file

i wish to convert an html document, which is stored as a string ($html) into PDF using convertAPI via CURL (ie no physical file)
i don't understand how I need to post the $html to the API, i was looking at the example on the convertapi webpage, but i don't seem to be able to make sense of it.
example pasted below.
$html = '<hmtl file contents>' ;
$parameters = array(
'Secret' => 'X?X?X?X?X?X?X',
);
function convert_api($src_format, $dst_format, $files, $parameters) {
$parameters = array_change_key_case($parameters);
$auth_param = array_key_exists('secret', $parameters) ? 'secret='.$parameters['secret'] : 'token='.$parameters['token'];
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER , false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_URL, "https://v2.convertapi.com/{$src_format}/to/{$dst_format}?{$auth_param}");
if (is_array($files)) {
foreach ($files as $index=>$file) {
$parameters["files[$index]"] = file_exists($file) ? new CurlFile($file) : $file;
}
} else {
$parameters['file'] = file_exists($files) ? new CurlFile($files) : $files;
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);
$response = curl_exec($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$error = curl_error($curl);
curl_close($curl);
if ($response && $httpcode >= 200 && $httpcode <= 299) {
return json_decode($response);
} else {
throw new Exception($error . $response, $httpcode);
}
}
thank you
Try this short example:
$secret = 'XXXXXXXXXX';
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/octet-stream', 'Accept: application/octet-stream', 'Content-Disposition: attachment; filename="file.html"'));
curl_setopt($curl, CURLOPT_URL, "https://v2.convertapi.com/html/to/pdf?secret=".$secret);
curl_setopt($curl, CURLOPT_POSTFIELDS, '<!doctype html><html lang=en><head><meta charset=utf-8><title>Conversion test</title></head><body>This is html body</body></html>');
$result = curl_exec($curl);
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == 200) {
file_put_contents("result.pdf", $result);
} else {
print("Server returned error:\n".$result."\n");
}
More examples can be found at: https://repl.it/#ConvertAPI

Instagram Latest post API suddenly stopped working

My instagram feed was working fine but it has stopped working. I am trying to find a solution. First I assume the issue is related to SSL, I even got SSL certificate for my website. But doesn't seem to work. Any help or suggestion is appreciated. Output showed latest image posted on instagram and now shows nothing.1
<div class="col-xs-12 col-sm-6 col-md-5 no-padding">
<div class="insta_img">
<?php
$instagram_id = get_field('instagram_id', 'option') ? get_field('instagram_id', 'option') : '';
$access_token = get_field('instagram_access_token', 'option') ? get_field('instagram_access_token', 'option') : '';
$url = "https://www.instagram.com/{$instagram_id}/?__a=1";
curl_close($ch);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$json = curl_exec($ch);
curl_close($ch);
$data = json_decode($json);
$data = $data->user->media->nodes;
$text = $data[0]->caption;
$created_time = $data[0]->date ;
$images = $data[0]->thumbnail_src;
/
?>
<img src="<?php echo $images; ?>" alt="instagram"/>
<div class="insta_content"><?php echo $text; ?>
<dt><?php echo date('d/m/Y', $created_time); ?></dt>
</div>
</div>
You're not correctly parsing the response. For example, in that response there is no user->media->nodes. You will need to check the response you get from the endpoint, and then parse it correctly to get the images:
$access_token = get_field('instagram_access_token', 'option') ? get_field('instagram_access_token', 'option') : '';
$url = "https://api.instagram.com/v1/users/self/media/recent?access_token={$access_token}";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$json = curl_exec($ch);
curl_close($ch);
$data = json_decode($json);
$data = $data->data;
$text = $data[0]->caption->text;
$created_time = $data[0]->created_time;
$images = $data[0]->images->standard_resolution->url;
Instagram has disabled following capabilities (i.e., deprecated following API's) on 6th April 2018:
Follower List - to read the list of followers and followed-by users
Relationships - to follow and unfollow accounts on a user’s behalf
Commenting on Public Content - to post and delete comments on a user’s behalf on public media
Likes - to like and unlike media on a user’s behalf
Subscriptions - to receive notifications when media is posted
Users Information - to search for and view users' public content
Some information on Public Content returned through hashtag and location
search - Name, Bio, Comments, Commenters, Follower Count, Following Count,
Post Count, and Profile Picture
The access token is working fine. But the post are not visible on the website. The code that I am using is as follow:
$access_token = get_field('instagram_access_token', 'option') ? get_field('instagram_access_token', 'option') : '';
$url = "https://api.instagram.com/v1/users/self/?access_token={$access_token}";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$json = curl_exec($ch);
curl_close($ch);
$data = json_decode($json);
$data = $data->user->media->nodes;
$text = $data[0]->caption;
$created_time = $data[0]->date ;
$images = $data[0]->thumbnail_src;

GET json with authentication API key

I'm making a script to be run by a cronjob.
It's suppossed to fetch some json orders and process them.
My script at the moment looks like this:
$json_string = '/admin/orders/7109.json';
$real_url = "https://my-store.myshopify.com{$json_string}";
$user = 'my-user';
$pass = 'my-pass';
$ch = curl_init($real_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $pass);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
$result = json_decode(curl_exec($ch),true);
curl_close($ch);
file_put_contents(__DIR__ . '/../debug/tracker_test.txt', print_r($result,true));
I'm getting this written into the code file even tho my credentials are correct.
Array
(
[errors] => [API] Invalid API key or access token (unrecognized login or wrong password)
)
Am I missing something ?
Edit: In the private apps section of Shopify it gives an example url format:
https://apikey:password#hostname/admin/resource.json
So now the script looks like this:
$json_url = 'https://my-api-key:my-api-pass#my-store.myshopify.com/admin/orders.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $real_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
$result = json_decode(curl_exec($ch), true);
curl_close($ch);
but I'm still getting the same error.
Turns out I had a syntax error.
I forgot to update the CURLOPT_URL with the new url variable.
It ended up looking like this:
$json_url = 'https://my-api-key:my-api-pass#my-store.myshopify.com/admin/orders.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $json_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
$result = json_decode(curl_exec($ch), true);
curl_close($ch);

Form Authentication, keep session Google Apps Script

I have a Google Apps script to communicate with an external program. The program wants an authentication to authenticate:
"The POST method should be used for this, and the content-type should be set to "application/x-www-form-urlencoded". The user name should be given in parameter "UserName" and the password should be given in parameter "Password". So the post data will be something like "UserName=xxx&Password=yyy". Keep in mind that when not sending the user name and password each request, that the session (or the object that keeps it) should be kept alive."
This is working:
var payload = "_UserName_="+username+"&_Password_="+password
var options = {
"method" : "POST",
"payload" : payload,
"contentType" : "application/x-www-form-urlencoded"
};
var xml = UrlFetchApp.fetch("https://start.application.nl/docs/XMLDivisions.aspx", options);
But here I want to make the next request:
var xml = UrlFetchApp.fetch("https://start.application.nl/docs/XMLDownload.aspx?PartnerKey="+partnerKey+"&Topic=Accounts");
but not authenticated?
How can I keep a GAS session alive?
A working example using PHP:
<?php
$baseurl = "https://start.exactonline.nl";
$username = "<username>";
$password = "<password>";
$partnerkey = ""; /* If you have one, the partnerkey with or without curly braces */
$division = "<division code>"; /* Check the result of the first division retrieving for the division codes */
$cookiefile = "cookie.txt";
$crtbundlefile = "cacert.pem"; /* this can be downloaded from http://curl.haxx.se/docs/caextract.html */
/* Logging in */
$header[1] = "Cache-Control: private";
$header[2] = "Connection: Keep-Alive";
/* init, don't term until you're completely done with this session */
$ch = curl_init();
/* Set all options */
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_CAINFO, $crtbundlefile);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("_UserName_"=>"$username", "_Password_"=>"$password"));
/* Retrieving divisions, the current should be the one in which the user worked the last time */
$url = "$baseurl/docs/XMLDivisions.aspx";
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
/* Switching divisions */
$url = "$baseurl/docs/ClearSession.aspx?Division=$division&Remember=3";
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
/* Retrieving divisions, the current should now be the one supplied in the switching divisions. This is only the current for this session! */
$url = "$baseurl/docs/XMLDivisions.aspx";
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
/* Upload an xml file */
$topic = "Invoices";
$url = "$baseurl/docs/XMLUpload.aspx?Topic=$topic&PartnerKey=$partnerkey";
curl_setopt($ch, CURLOPT_URL, $url);
/* Send the xml along with the request */
$filename = "Invoices.xml";
$fp = fopen($filename, "r");
$xml = fread($fp, filesize($filename));
curl_setopt($ch, CURLOPT_POSTFIELDS, utf8_encode($xml));
$result = curl_exec($ch);
/* Finally close as we're finished with this session */
curl_close($ch);
echo $result;
?>
I think you are missing the cookie.
Try something like this:
var payload = "_UserName_="+username+"&_Password_="+password
var options = {
"method" : "POST",
"payload" : payload,
"contentType" : "application/x-www-form-urlencoded"
};
var xml = UrlFetchApp.fetch("https://start.application.nl/docs/XMLDivisions.aspx", options);
var cookie = response.getAllHeaders()['Set-Cookie'];
Then, send the cookie in the second request along your other prepared headers.
var header = {'Cookie':cookie};
var opt = {"headers":header};
var xml = UrlFetchApp.fetch("https://start.application.nl/docs/XMLDownload.aspx?PartnerKey="+partnerKey+"&Topic=Accounts",opts);
I think that should work, but sometimes the server needs not only the session in the cookie, but also the same IP address should make the request, in that case google IP's sometimes change...

Craigslist, CURL, Simple PHP DOM Issues

I am logging into Craigslist with CURL to scrape the status of my posted listings. The problem I encounter is the transfer of HTML from CURL $output to file_get_html. While Craigslist statuses are actually nested inside TR elements, I just wanted to test the most basic functions to see if things were getting passed through (i.e. link scraping). They are not.
For example, this doesn't work:
$cookie_file_path = getcwd()."/cookie.txt";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://accounts.craigslist.org/login?LoginType=L&step=confirmation&originalURI=%2Flogin&rt=&rp=&inputEmailHandle='.$email.'&inputPassword='.$password.'&submit=Log%20In');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.craigslist.org');
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo $output;
//
include_once('simple_html_dom.php');
$html = file_get_html($output);
//find all links
foreach($html->find('a') as $element)
echo $element->href . '<br>';
I know the expression works because it returns links if I put in 'http://google.com', or something or other.
This is how it should be done
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.sitename.com');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
$str = curl_exec($curl);
curl_close($curl);
$html= str_get_html($str);
Shouldn't you be using str_get_html instead of file_get_html?
Since $ouput is a string!