How to Create CKReference using CloudKit Web Services? - php

I've using CK Web services very successfully, but I am stumped about how to create a CKReference.
I've looked at the docs here re Reference Dictionaries, but can't make such a dictionary work.
My php generates the following operations dictionary:
{"operations":[
{"operationType": "create",
"record": { "recordType": "Works",
"fields": {
"type":{"value":"Painting"},
"title": {"value":"test"},
"date": {"value":"10/29/1965"},
"height": {"value":"21"},
"length": {"value":"21"},
"width": {"value":"21"},
"runningTime": {"value":""},
"materials": {"value":"test"},
"description":{"value":"test"},
"saleStatus": {"value":"yes"},
"tos":{"value":"yes"},
"artist": {"value":"Peter Wiley"},
"artistRecordName":{"value":"286CB3BF-69CC-4DD3-9233-CC80E5FA95D4"},
"artistRecordRef": {
"recordName": {"value":"286CB3BF-69CC-4DD3-9233-CC80E5FA95D4"},
"zoneID":{"zoneName": {"value":"_defaultZone"}},
"action": {"value":"NONE"}
},
"subject":{"value":""},
"metaType":{"value":"Fine Art"},
"userRecordName":{"value":"30C54AD8-3701-428C-99B7-0393DD2DAB45"},
"userRole":{"value":"Artist"},
"status":{"value":"P"}
}
} }
]}
This request returns the error:
BAD_REQUEST" [1]=> string(62) "BadRequestException: Unexpected input
at [line: 26, column: 3]
If I remove the "artistRecordRef" the request works as it should.
I am sure the answer is obvious to a more experienced eye. Can someone see what's wrong?

OK, I found the answer here, but have posted for others who may have the question because the answer was not easy to find.
This is what works:
"artistRecordRef": {"value": {
"recordName": "'.$artistRecordName.'",
"action": "NONE"
}
},
The Reference Dictionary has to be passed as a value. I didn't get this and it's not well documented with examples in the Apple docs (at least in those I was able to find).
See: How can I use CloudKit web services to query based on a reference field?

Related

How to get form fields in Facebook Lead Ads API?

I'm using Facebook Lead Ads API. I need to retrieve all fields from a form by ID. I know I can:
get all forms by calling /<PAGE_ID>/leadgen_forms, but it doesn't return the fields
get a form by /<FORM_ID>, but it displays only the name and a few
data, but not fields
get all leads by /<FORM_ID>/leads - it gives me the fields in each
lead, but only if I have leads; there's also another problem with this solution - the order of the fields is random
Is there any dedicated way to retrieve leadgen form fields, even when there are no leads yet?
I found out that I can download the CSV and in the first row, it gives me all fields IDs (and some other columns). I'm not sure though how I can read the content of this file in PHP, because it gives me some HTML when I try to use get_file_contents() on it.
You can get these by adding non-default field questions, so the url becomes /<form_id>?fields=id,name,questions.
The official docs don't describe the fields available for reading but the questions field and its nested fields are described in the params used for creating a lead form.
Example output
{
"id": "1234567890000",
"name": "test form",
"questions": [
{
"key": "name",
"label": "Full Name",
"type": "FULL_NAME",
"id": "777888999000111"
},
{
"key": "email",
"label": "Email Address",
"type": "EMAIL",
"id": "111222333444555"
}
]
}
Just a warning since this answer comes first on google search.
Since Facebook API v5.0 field "qualifiers" is removed and will throw an error.
Replace it with "questions" which is similar (if not exact) syntax as qualifiers. Found out the hard way on production server...

AtTask API - Adding Time Off Hours

I'm using the Chrome Advanced Rest Client to test the AtTask API. I'm getting a lot of stuff figured out, but also getting some unexpected results. The latest is when adding new records to the AtTask Time Off Calendar.
I am able to easily add time off to the calendar. I am use the POST method, with the following URL:
https://COMPANY.attasksandbox.com/attask/api/v4.0/resvt?sessionID=SESSIONIDGOESHERE&userID=USERIDGOESHERE&startDate=2014-11-24T00:00:00&endDate=2014-11-28T23:59:59
This mark all the days between 11/24 through 11/28 as time off. Great, so far. The problem is that it removes all other rime-off records for the specified user. I am not issuing a DELETE, so I'm not understanding whey the records are being deleted. More importantly, I'm not understanding how to keep them from being deleted.
Once again, thanks in advance.
Time-off in attask is stored as a collection and when you make an edit to a collection it will replace the collection data with the date provided in the update. This is why your call is removing existing data.
In order to add a new time-off you will need to make 2 calls 1 to get exsisting time-off and one to enter the data back with the new dates.
note-I am using my own data so dates are a bit different for me but concept is same
Your get call will be
GET /api/resvt/search?userID=[userID]&fields=endDate,startDate,ID
which returns something like
{
"data": [
{
"ID": "547debb6000dea62198bd66b7c73e174",
"objCode": "RESVT",
"endDate": "2014-07-08T23:59:00:163-0600",
"startDate": "2014-07-08T00:00:00:163-0600"
},
{
"ID": "547debb6000dea61b8c695ba24918fe8",
"objCode": "RESVT",
"endDate": "2014-02-13T23:59:00:329-0700",
"startDate": "2014-02-13T00:00:00:329-0700"
}
]
}
Once you have this you can add your new time off to the collection using an updates command on the user object. Notice you are providing IDs to the time-off that is already in the system and the new time-Off you are providing no ID
PUT /attask/api/v4.0/user/[userID]?&sessionID=[sessionID]&updates={reservedTimes: [ { "ID": "547debb6000dea62198bd66b7c73e174", "objCode": "RESVT", "endDate": "2014-07-08T23:59:00:163-0600", "startDate": "2014-07-08T00:00:00:163-0600" }, { "ID": "547debb6000dea61b8c695ba24918fe8", "objCode": "RESVT", "endDate": "2014-02-13T23:59:00:329-0700", "startDate": "2014-02-13T00:00:00:329-0700" }, { "objCode": "RESVT", "endDate": "2014-02-14T23:59:00:329-0700", "startDate": "2014-02-14T00:00:00:329-0700" } ] }
This is a bit bulky and complex but is the only way to do this in the API at this time.

Scraping .php websites

I'v been having trouble scraping the following website content: http://www.qe.com.qa/wp/mw/MarketWatch.php
using file_get_contents() never gets me the right tag. I would like to scrape the content of the following tag: td aria-describedby="grid_OfferPrice"
is the website protected from scraping? because when I try the same method with diffrent websites it works. if yes, then what is a good work around for this ?
The way to see if scraping works is to output what file_get_contents returns. If you have nothing back or an error then maybe your IP has been restricted by their admin.
If it returns their source code then it's working but maybe the tag you're looking for has not been found.
Eliminate failures in your process by answering these questions first, one at a time.
I viewed their source code and the aria attribute you are searching for doesn't appear to exist.
It seems they load the data on that page from another source which is at this page (http://www.qe.com.qa/wp/mw/bg/ReadData.php?Types=SELECTED&iType=SO&dummy=1401401577192&_search=false&nd=1401401577279&rows=100&page=1&sidx=&sord=asc)
If you want the data from that page then use file_get_contents on it directly.
The data from that page in an online json editor gives you a neat way of quickly seeing whether this is a useful solution for you.
A sample of that data is listed below:
{
"total": "140",
"page": "1",
"records": "140",
"rows": [
{
"Topic": "QNBK/NM",
"Symbol": "QNBK",
"CompanyEN": "QNB",
"CompanyAR": "QNB",
"Trend": "-",
"StateEN": "Tradeable",
"StateAR": "المتداوله",
"CatEN": "Listed Companies",
"CatAR": "الشركات المدرجة",
"SectorEN": "Banks & Financial Services",
"SectorAR": "البنوك والخدمات المالية",
"ShariahEN": "N/A",
"ShariahAR": "N/A",
"OfferVolume": "7503",
"OfferPrice": "184.00",
"BidPrice": "182.00",
"BidVolume": "15807",
"OpenPrice": "190.0",
"High": "191.7",
"Low": "181.0",
"IMP": "182.0",
"LastPrice": "182.0",
"PrevClosing": "187.0",
"Change": "-5.0",
"PercentChange": "-2.6737",
"Trades": "980",
"Volume": "2588830",
"W52High": "199.0",
"W52Low": "145.0",
"Value": "481813446.4"
},
{
"Topic": "QIBK/NM",
"Symbol": "QIBK",
"CompanyEN": "Qatar Islamic Bank",
"CompanyAR": "المصرف ",
"Trend": "+",
"StateEN": ...
Make sure you read this link about 'scraping' etiquette.
Link: http://simplehtmldom.sourceforge.net/
$dom = new DOMDocument();
$dom->loadHTML(file_get_contents("EXAMPLE.COM");
$items = $dom->getElementsByTagName("YOUR TAG");
This class allows you to search HTML code for elements. I have used it a few times before and it is by far the best solution I have found for your issue.

Google Cloud Printing and Capabilities PPD

We are having some success printing via Googles Cloud Print service. But wondering if anyone has information regarding the capabilities parameter when submitting a job to print and some pointers in how to create and work this format which I believe is ppd.
We have been able to get the capabilities of the printer via using the method http://www.google.com/cloudprint/printer which returns all the values for our printer. The problem is we don't quite understand what we are meant to do with this in order to define the capability options we would like to print with. This would include options for the copies of pages printed, paper type and print quality. An example of the capabilities information we can receive is like this :
{
"name": "copies",
"displayName": "Copies",
"type": "ParameterDef"
}
{
"UIType": "PickOne",
"name": "HPEconoMode",
"displayName": "EconoMode",
"type": "Feature",
"options": [
{
"ppd:value": "\"\"",
"default": true,
"name": "PrinterDefault",
"displayName": "Printer's Current Setting"
},
{
"ppd:value": "\u003c\u003c/EconoMode true\u003e\u003e setpagedevice",
"name": "True",
"displayName": "Save Toner"
},
{
"ppd:value": "\u003c\u003c/EconoMode false\u003e\u003e setpagedevice",
"name": "False",
"displayName": "Highest Quality"
}
]
}
The GCP documentation is badly lacking in this regard. Anyway, I've managed to find that the correct parameter to send printer settings is ticket, not capabilities. The first part of the parameters corresponds to the basic settings from the print dialog and they are quite self-explanatory and the values are easy to change. The vendor_ticket_item array is a bit more complicated. It contains id/value pairs described by the printer capabilities. The id will contain the name of the parameter from the capabilities and the value will contain the name of one of the records in the parameter options, or a numeric value etc, as described in the capabilities.
For mode details please take a look at my full solution.
{
"version":"1.0",
"print":{
"color":{"vendor_id":"psk:Color","type":0},
"duplex":{"type":0},
"page_orientation":{"type":1},
"copies":{"copies":1},
"dpi":{"horizontal_dpi":600,"vertical_dpi":600},
"media_size":{"width_microns":148000,"height_microns":210000,"is_continuous_feed":false},
"collate":{"collate":true}
,
"vendor_ticket_item":[
//Printer specific settings here, from the capabilities:
{"id":"psk:JobInputBin","value":"ns0000:Tray3"},
{"id":"psk:PageICMRenderingIntent","value":"psk:Photographs"},
{"id":"psk:PageMediaType","value":"ns0000:Auto"},
{"id":"psk:JobOutputBin","value":"ns0000:Auto"},
//etc.
]
}
}

How to recognise adult content programmatically?

I am currently developing a website for a client. It consists of users being able to upload pictures to be shown in a gallery on the site.
The problem we have is that when a user uploads an image it would obviously need to be verified to make sure it is safe for the website (no pornographic or explicit pictures). However my client would not like to manually have to accept every image that is being uploaded as this would be time consuming and the users' images would not instantly be online.
I am writing my code in PHP. If needs be I could change to ASP.net or C#. Is there any way that this can be done?
2019 Update
A lot has changed since this original answer way back in 2013, the main thing being machine learning. There are now a number of libraries and API's available for programmatically detecting adult content:
Google Cloud Vision API, which uses the same models Google uses for safe search.
NSFWJS uses TensorFlow.js claims to achieve ~90% accuracy and is open source under MIT license.
Yahoo has a solution called Open NSFW under the BSD 2 clause license.
2013 Answer
There is a JavaScript library called nude.js which is for this, although I have never used it. Here is a demo of it in use.
There is also PORNsweeper.
Another option is to "outsource" the moderation work using something like Amazon Mechanical Turk, which is a crowdsourced platform which "enables computer programs to co-ordinate the use of human intelligence to perform tasks which computers are unable to do". So you would basically pay a small amount per moderation item and have an outsourced actual human to moderate the content for you.
The only other solution I can think of is to make the images user moderated, where users can flag inappropriate posts/images for moderation, and if nobody wants to manually moderate them they can simply be removed after a certain number of flags.
Here are a few other interesting links on the topic:
http://thomas.deselaers.de/publications/papers/deselaers_icpr08_porn.pdf
http://www.naun.org/multimedia/NAUN/computers/20-462.pdf
What is the best way to programmatically detect porn images?
The example below does not give you 100% accurate results but it should help you a least a bit and works out of the box.
<?php
$url = 'http://server.com/image.png';
$data = json_decode(file_get_contents('http://api.rest7.com/v1/detect_nudity.php?url=' . $url));
if (#$data->success !== 1)
{
die('Failed');
}
echo 'Contains nudity? ' . $data->nudity . '<br>';
echo 'Nudity percentage: ' . $data->nudity_percentage . '<br>';
If you are looking for an API-based solution, you may want to check out Sightengine.com
It's an automated solution to detect things like adult content, violence, celebrities etc in images and videos.
Here is an example in PHP, using the SDK:
<?php
$client = new SightengineClient('YourApplicationID', 'YourAPIKey');
$output = $client>check('nudity')>image('https://sightengine.com/assets/img/examples/example2.jpg');
The output will then return the classification:
{
"status": "success",
"request": {
"id": "req_VjyxevVQYXQZ1HMbnwtn",
"timestamp": 1471762434.0244,
"operations": 1
},
"nudity": {
"raw": 0.000757,
"partial": 0.000763,
"safe": 0.999243
},
"media": {
"id": "med_KWmB2GQZ29N4MVpVdq5K",
"uri": "https://sightengine.com/assets/img/examples/example2.jpg"
}
}
Have a look at the documentation for more details: https://sightengine.com/docs/#nudity-detection
(disclaimer: I work there)
There is a free API that detects adult content (porn, nudity, NSFW).
https://market.mashape.com/purelabs/sensitive-image-detection
We've using it on our production environment and I would say it works pretty good so far. There are some false detections though, it seems they prefer to mark the image as unsafe if they are unsure.
It all depends on the level of accuracy you are looking for, simple skin tone detection (like nude.js) will prob get you 60-80% accuracy on a generous sample set, for anything more accurate than that, let's say 90-95%, you are going to need some specialized computer vision system with an evolving model that is revised over time. For the latter you might want to check out http://clarifai.com or https://scanii.com (which I work on)
Microsoft Azure has a very cool API called Computer Vision, which you can use for free (either through the UI or programmatically) and has tons of documentation, including for PHP.
It has some amazingly accurate (and sometimes humorous) results.
Outside of detecting adult and "racy" material, it will read text, guess your age, identify primary colours, etc etc.
You can try it out at azure.microsoft.com.
Sample output from a "racy" image:
FEATURE NAME: VALUE:
Description { "tags": [ "person", "man", "young", "woman", "holding",
"surfing", "board", "hair", "laying", "boy", "standing",
"water", "cutting", "white", "beach", "people", "bed" ],
"captions": [ { "text": "a man and a woman taking a selfie",
"confidence": 0.133149087 } ] }
Tags [ { "name": "person", "confidence": 0.9997446 },
{ "name": "man", "confidence": 0.9587285 },
{ "name": "wall", "confidence": 0.9546831 },
{ "name": "swimsuit", "confidence": 0.499717563 } ]
Image format "Jpeg"
Image dimensions 1328 x 2000
Clip art type 0
Line drawing type 0
Black and white false
Adult content true
Adult score 0.9845981
Racy true
Racy score 0.964191854
Categories [ { "name": "people_baby", "score": 0.4921875 } ]
Faces [ { "age": 37, "gender": "Female",
"faceRectangle": { "top": 317, "left": 1554,
"width": 232, "height": 232 } } ]
Dominant color background "Brown"
Dominant color foreground "Black"
Accent Color #0D8CBE

Categories