Jump to content

Using API to add content in Pages


Recommended Posts

Posted

Hello,

We are trying to use the REST API to add datas to a Pages DB in PHP with cURL .

I don't understand how to fill the "Fields" parameter. The API reference says that I must pass a "Bejct object" (a bug maybe ?)

Do you have an example for me ?

Thanks in advance for your replies ^_^

Posted

Yes, it's a typo.  They have a typo in their doc block for the REST API.

It should be an object of fields.   See \IPS\cms\Fields.  /applications/cms/sources/Fields/Fields.php

Posted

Honestly, digging through the source code is likely your best bet here.  API's such as this are something that's a gray area with support.  They provide the endpoint, with little documentation, and don't support custom development that use the endpoint.

Posted
	protected function _createOrUpdate( \IPS\Content\Item $item )
	{
		/* Set field values */
		if ( isset( \IPS\Request::i()->fields ) )
		{
			$fieldsClass = str_replace( 'Records', 'Fields', get_class( $item ) );
			foreach ( $fieldsClass::data() as $key => $field )
			{
				$key = "field_{$field->_id}";
				$item->$key = \IPS\Request::i()->fields[ $field->id ];
			}
		}
		
		/* Pass up */
		return parent::_createOrUpdate( $item );
	}

I'll so some of the legwork for you.  /applications/cms/api/records.php.  This is where it's using the field data you're sending.  

	/**
	 * POST /cms/records/{database_id}
	 * Create a record
	 *
	 * @param		int					$database			Database ID Number
	 * @reqapiparam	int					category			The ID number of the category the record should be created in. If the database does not use categories, this is not required
	 * @reqapiparam	int					author				The ID number of the member creating the record (0 for guest)
	 * @reqapiparam	obejct				fields				Field values. Keys should be the field ID, and the value should be the value
	 * @apiparam	string				prefix				Prefix tag
	 * @apiparam	string				tags				Comma-separated list of tags (do not include prefix)
	 * @apiparam	datetime			date				The date/time that should be used for the record date. If not provided, will use the current date/time
	 * @apiparam	string				ip_address			The IP address that should be stored for the record. If not provided, will use the IP address from the API request
	 * @apiparam	int					locked				1/0 indicating if the record should be locked
	 * @apiparam	int					hidden				0 = unhidden; 1 = hidden, pending moderator approval; -1 = hidden (as if hidden by a moderator)
	 * @apiparam	int					pinned				1/0 indicating if the record should be pinned
	 * @apiparam	int					featured			1/0 indicating if the record should be featured
	 * @throws		2T306/4				INVALID_DATABASE	The database ID does not exist
	 * @throws		1T306/5				NO_CATEGORY			The category ID does not exist
	 * @throws		1T306/6				NO_AUTHOR			The author ID does not exist
	 * @return		\IPS\cms\Records
	 */

 

The important bit.

Quote

     * @reqapiparam    obejct                fields                Field values. Keys should be the field ID, and the value should be the value

 

Posted

I finally did it, it's easier than that :

Assume that you want to add the following :

$postdata = array();
$postdata["author"] = 1; 
$postdata["category"] = 1; 
$postdata["fields"] = array();
$postdata["fields"]["90"] = "My title";

Before sending it to curl, you have to use http_build_query and urldecode :

$postdata = urldecode(http_build_query($postdata));

Then, you just have to :

$curl = curl_init( $apiUrl );
curl_setopt_array( $curl, array(
	CURLOPT_RETURNTRANSFER	=> TRUE,
	CURLOPT_HTTPAUTH    => CURLAUTH_BASIC,
	CURLOPT_USERPWD	    => "{$IPSApiKey}:",
	CURLOPT_POST		=> true,
	CURLOPT_POSTFIELDS  => $postdata
	));

And... It works !

Now, I try to add some files to it. Any idea ?

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...