Download the PHP package sanity/sanity-php without Composer
On this page you can find all versions of the php package sanity/sanity-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package sanity-php
sanity-php
PHP library for the Sanity API
Requirements
sanity-php requires PHP >= 5.6, with the json
module installed.
Composer
You can install the library via Composer. Run the following command:
To use the library, use Composer's autoload:
Usage
Instantiating a new client
Using an authorization token
Specifying API version
Sanity uses ISO dates (YYYY-MM-DD) in UTC timezone for versioning. The explanation for this can be found in the documentation
In general, unless you know what API version you want to use, you'll want to set it to todays UTC date. By doing this, you'll get all the latest bugfixes and features, while preventing any timezone confusion and locking the API to prevent breaking changes.
Note: Do not be tempted to use a dynamic value for the apiVersion
. The whole reason for setting a static value is to prevent unexpected, breaking changes.
In future versions, specifying an API version will be required. For now, to maintain backwards compatiblity, not specifying a version will trigger a deprecation warning and fall back to using v1
.
Fetch a single document by ID
Performing queries
See the query documentation for more information on how to write queries.
Using perspectives
The perspective
option can be used to specify special filtering behavior for queries. The default value is raw
, which means no special filtering is applied, while previewDrafts
can be used to optimize for specific use cases.
published
Useful for when you want to be sure that draft documents are not returned in production. Pairs well with private datasets.
With a dataset that looks like this:
And a query like this:
Then $authors
will only contain documents that don't have a drafts.
prefix in their _id
, in this case just "George Martin":
previewDrafts
Designed to help answer the question "What is our app going to look like after all the draft documents are published?".
Given a dataset like this:
And a query like this:
Then authors
will look like this. Note that the result dedupes documents with a preference for the draft version:
Since the query simulates what the result will be after publishing the drafts, the _id
doesn't contain the drafts.
prefix. If you want to check if a document is a draft or not you can use the _originalId
field, which is only available when using the previewDrafts
perspective.
Which changes the result to be:
Creating documents
This creates a new document with the given properties. It must contain a _type
attribute, and may contain a _id
attribute. If an ID is specified and a document with that ID already exist, the mutation will fail. If an ID is not specified, it will be auto-generated and is included in the returned document.
Creating a document (if it does not exist)
As noted above, if you include an _id
property when calling create()
and a document with this ID already exists, it will fail. If you instead want to ignore the create operation if it exists, you can use createIfNotExists()
. It takes the same arguments as create()
, the only difference being that it requires an _id
attribute.
Replacing a document
If you don't care whether or not a document exists already and just want to replace it, you can use the createOrReplace()
method.
Patch/update a document
Todo: Document all patch operations
Delete a document
Multiple mutations in a transaction
Clientless patches & transactions
An important note on this approach is that you cannot call commit()
on transactions or patches instantiated this way, instead you have to pass them to client.mutate()
.
Upload an image asset (from local file)
Upload an image asset (from a string)
Upload image, extract exif and palette data
Upload a file asset (from local file)
Upload a file asset (from a string)
Referencing an uploaded image/file
Upload image and append to array
Get client configuration
Set client configuration
The new configuration will be merged with the existing, so you only need to pass the options you want to modify.
Rendering block content
When you use the block editor in Sanity, it produces a structured array structure that you can use to render the content on any platform you might want. In PHP, a common output format is HTML. To make the transformation from the array structure to HTML simpler, we include a helper class for this within the library.
If your content only contains the basic, built-in block types, you can get rendered HTML like this:
If you have some custom types, or would like to customize the rendering, you may pass an associative array of serializers:
Contributing
sanity-php
follows the PSR-2 Coding Style Guide. Contributions are welcome, but must conform to this standard.
License
MIT-licensed. See LICENSE