{"id":54,"date":"2009-08-25T17:40:18","date_gmt":"2009-08-25T22:40:18","guid":{"rendered":"http:\/\/blogs.wp.stage.cpanel.net\/2009\/08\/api_basics_and_how_to_call_api1_functions_2\/"},"modified":"2009-08-25T17:40:18","modified_gmt":"2009-08-25T22:40:18","slug":"api_basics_and_how_to_call_api1_functions_2","status":"publish","type":"post","link":"https:\/\/devel.www.cpanel.net\/blog\/products\/api_basics_and_how_to_call_api1_functions_2\/","title":{"rendered":"API basics and how to call API1 functions"},"content":{"rendered":"

I have been noticing several people challenged with calling cPanel functions via our various ways of hooking into our APIs. Unfortunately, this isn’t as cut and dry as just calling a function within a programming language.\u00a0 Various factors, such as whether the call is being made from within cPanel or from a remote system, affect how this needs to be done.<\/p>\n

To help you understand this, I will begin covering these topics in a series of posts on cPanel’s various functions and how they work. In this first post of the series, I will discuss the basics of cPanel’s APIs, and how to call API1 functions.<\/p>\n

The basics<\/strong>
\nBefore we can even begin going over how to call cPanel’s API functions, we need to discuss the various API types.<\/p>\n

For the most part, cPanel’s API is divided into two sub-systems; API1 and API2. The difference between these is in how they are called and how they return data.<\/p>\n

API1<\/strong> will normally print data to the cPanel interface.\u00a0 This works well when the functions are called via cpanel<\/em> tags (covered later in this article), but won’t return much useful data when called via the XML API, livePHP or CGI scripts.<\/p>\n

API2<\/strong>, on the other hand, is a much more robust system, capable of returning complex data structures that can be parsed into templates contained within a cp<\/em> tag. API2 calls, as they do actually return data, will always return relevant information when called via the XML API, livePHP or CGI scripts.<\/p>\n

One of my favorite features of API2 is that it uses named-based parameters that translate well into URL parameters.<\/p>\n

Calling API1 functions<\/strong>
\nAPI1 functions can be called via a tag. As previously mentioned, these print data to the cPanel interface. cpanel<\/em> tags use the following format:
\n<cpanel Module=\"function( params )\">\u2028<\/code><\/p>\n

So, if you wanted to call Ftp::ftpservername()<\/em>, which is used to print out which FTP server is being used, it would be called with the following syntax:
\n<cpanel Ftp=\"ftpservername()\"><\/code><\/p>\n

Now, you will not want all functions to actually display data. For example, Mysql::adddb<\/em> is generally something that you do not want printing data, for security reasons. Instead you want to check for error handling.<\/p>\n

Any data being printed from this function can be suppressed in the browser via HTML comments, like so:
\n<!--Module=\"function()\"-->\u2028<\/code><\/p>\n

Sending input to the API1 function<\/strong>
\nSo, in order to pass data to an API function, we will need a way to pull in the data.<\/p>\n

Inside of cPanel’s HTML parsing, we have access to certain variables. The main variables that you need to be aware of are $FORM<\/em> and $CPERROR<\/em>. <\/em><\/p>\n

$FORM<\/em> is merely a variable that is populated with either GET<\/em> or POST<\/em> data passed to the page from the browser. This variable is how cPanel passes data around from page to page. To access it, you call the $FORM<\/em> variable the same way that you would call a hash in Perl ($FORM{‘element’}<\/em>).<\/p>\n

So, for example, if you had a page called add_mysql_db<\/em> that was passed the following:
\nadd_mysql_db.html?db=dbname<\/code><\/p>\n

It would contain the following:
\n<cpanel Mysql=\"addb( $FORM{'db'} )\"><\/code><\/p>\n

The <cpanelif><\/em> tag<\/strong>
\nAlong with the cpanel<\/em> tag, there is also support for some really basic logic within this system, via the <cpanelif><\/em> tag. This tag allows for checking of basic boolean logic to see if a variable is set; if true, it will send whatever is contained between the <cpanelif><\/em> tags to the browser.\u00a0 <cpanelif> tags cannot be nested in any way.<\/p>\n

To call cpanelif<\/em>, you will do something like the following:
\n<cpanelif $VAR{''}><\/code><\/p>\n

HTML CODE HERE<\/p>\n

<\/cpanelif><\/p>\n

This is useful when you want to display an error message within cPanel’s interface.\u00a0 These are populated into the $CPERROR{$context} <\/em>variable.<\/p>\n

Unfortunately, context is defined on the back-end on a per-module basis, so generally $context<\/em> will be the same as whatever module you are calling.\u00a0 In the case of our previous MySQL example, if we wanted to check for an error message, we would want to do the following:
\n<cpanel Mysql=\"adddb( $FORM{'db'} )\"><\/code>
\n<cpanelif $CPERROR{‘mysql’}><\/p>\n

ERROR: $CPERROR{‘mysql’}<\/p>\n

<\/cpanelif><\/p>\n

Of course, you would want to also want to do !$CPERROR{‘mysql’} <\/em>around any success messages to make sure you don’t end up with “ERROR: errormsg This was successful”.<\/p>\n

These are the basics of how to call API1 via .html<\/em> files. You can always use the XML API to call these functions as well, but because this is API1, you may not get any useful data.<\/p>\n","protected":false},"excerpt":{"rendered":"

I have been noticing several people challenged with calling cPanel functions via our various ways of hooking into our APIs. Unfortunately, this isn’t as cut and dry as just calling a function within a programming language.\u00a0 Various factors, such as whether the call is being made from within cPanel or from a remote system, affect […]<\/p>\n","protected":false},"author":77,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[49],"tags":[],"class_list":["post-54","post","type-post","status-publish","format-standard","hentry","category-products"],"acf":[],"yoast_head":"\nAPI basics and how to call API1 functions | cPanel<\/title>\n <!-- Mirrored from cpanel.net/wp-json/wp/v2/posts/54/ by HTTrack Website Copier/3.x [XR&CO'2014], Fri, 06 Sep 2024 09:21:29 GMT --> <meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=_-702.html \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"API basics and how to call API1 functions | cPanel\" \/>\n<meta property=\"og:description\" content=\"I have been noticing several people challenged with calling cPanel functions via our various ways of hooking into our APIs. Unfortunately, this isn’t as cut and dry as just calling a function within a programming language.\u00a0 Various factors, such as whether the call is being made from within cPanel or from a remote system, affect […]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/devel.www.cpanel.net\/blog\/products\/api_basics_and_how_to_call_api1_functions_2\/\" \/>\n<meta property=\"og:site_name\" content=\"cPanel\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cpanel\/\" \/>\n<meta property=\"article:published_time\" content=\"2009-08-25T22:40:18+00:00\" \/>\n<meta name=\"author\" content=\"cPanel Community\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@cPanel\" \/>\n<meta name=\"twitter:site\" content=\"@cPanel\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"cPanel Community\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/devel.www.cpanel.net\/blog\/products\/api_basics_and_how_to_call_api1_functions_2\/\",\"url\":\"https:\/\/devel.www.cpanel.net\/blog\/products\/api_basics_and_how_to_call_api1_functions_2\/\",\"name\":\"API basics and how to call API1 functions | cPanel\",\"isPartOf\":{\"@id\":\"https:\/\/devel.www.cpanel.net\/#website\"},\"datePublished\":\"2009-08-25T22:40:18+00:00\",\"dateModified\":\"2009-08-25T22:40:18+00:00\",\"author\":{\"@id\":\"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/8cf97408aad4fb70cf55d11a1d4f57f8\"},\"breadcrumb\":{\"@id\":\"https:\/\/devel.www.cpanel.net\/blog\/products\/api_basics_and_how_to_call_api1_functions_2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/devel.www.cpanel.net\/blog\/products\/api_basics_and_how_to_call_api1_functions_2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/devel.www.cpanel.net\/blog\/products\/api_basics_and_how_to_call_api1_functions_2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/devel.www.cpanel.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"API basics and how to call API1 functions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/devel.www.cpanel.net\/#website\",\"url\":\"https:\/\/devel.www.cpanel.net\/\",\"name\":\"cPanel\",\"description\":\"Hosting Platform of Choices\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/devel.www.cpanel.net\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/8cf97408aad4fb70cf55d11a1d4f57f8\",\"name\":\"cPanel Community\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e1949945083b5526bb95711bd3d616b3?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e1949945083b5526bb95711bd3d616b3?s=96&d=mm&r=g\",\"caption\":\"cPanel Community\"},\"description\":\"The web hosting industry's most reliable management solution since 1997. With our first-class support and rich feature set, it's easy to see why our customers and partners make cPanel & WHM their hosting platform of choice. For more information, visit cPanel.net.\",\"sameAs\":[\"https:\/\/cpanel.net\"],\"url\":\"https:\/\/devel.www.cpanel.net\/blog\/author\/cpadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"API basics and how to call API1 functions | cPanel","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/devel.www.cpanel.net\/blog\/products\/api_basics_and_how_to_call_api1_functions_2\/","og_locale":"en_US","og_type":"article","og_title":"API basics and how to call API1 functions | cPanel","og_description":"I have been noticing several people challenged with calling cPanel functions via our various ways of hooking into our APIs. Unfortunately, this isn’t as cut and dry as just calling a function within a programming language.\u00a0 Various factors, such as whether the call is being made from within cPanel or from a remote system, affect […]","og_url":"https:\/\/devel.www.cpanel.net\/blog\/products\/api_basics_and_how_to_call_api1_functions_2\/","og_site_name":"cPanel","article_publisher":"https:\/\/www.facebook.com\/cpanel\/","article_published_time":"2009-08-25T22:40:18+00:00","author":"cPanel Community","twitter_card":"summary_large_image","twitter_creator":"@cPanel","twitter_site":"@cPanel","twitter_misc":{"Written by":"cPanel Community","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/devel.www.cpanel.net\/blog\/products\/api_basics_and_how_to_call_api1_functions_2\/","url":"https:\/\/devel.www.cpanel.net\/blog\/products\/api_basics_and_how_to_call_api1_functions_2\/","name":"API basics and how to call API1 functions | cPanel","isPartOf":{"@id":"https:\/\/devel.www.cpanel.net\/#website"},"datePublished":"2009-08-25T22:40:18+00:00","dateModified":"2009-08-25T22:40:18+00:00","author":{"@id":"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/8cf97408aad4fb70cf55d11a1d4f57f8"},"breadcrumb":{"@id":"https:\/\/devel.www.cpanel.net\/blog\/products\/api_basics_and_how_to_call_api1_functions_2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/devel.www.cpanel.net\/blog\/products\/api_basics_and_how_to_call_api1_functions_2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/devel.www.cpanel.net\/blog\/products\/api_basics_and_how_to_call_api1_functions_2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/devel.www.cpanel.net\/"},{"@type":"ListItem","position":2,"name":"API basics and how to call API1 functions"}]},{"@type":"WebSite","@id":"https:\/\/devel.www.cpanel.net\/#website","url":"https:\/\/devel.www.cpanel.net\/","name":"cPanel","description":"Hosting Platform of Choices","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/devel.www.cpanel.net\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/8cf97408aad4fb70cf55d11a1d4f57f8","name":"cPanel Community","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e1949945083b5526bb95711bd3d616b3?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e1949945083b5526bb95711bd3d616b3?s=96&d=mm&r=g","caption":"cPanel Community"},"description":"The web hosting industry's most reliable management solution since 1997. With our first-class support and rich feature set, it's easy to see why our customers and partners make cPanel & WHM their hosting platform of choice. For more information, visit cPanel.net.","sameAs":["https:\/\/cpanel.net"],"url":"https:\/\/devel.www.cpanel.net\/blog\/author\/cpadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/posts\/54"}],"collection":[{"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/users\/77"}],"replies":[{"embeddable":true,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/comments?post=54"}],"version-history":[{"count":0,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/posts\/54\/revisions"}],"wp:attachment":[{"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/media?parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/categories?post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/tags?post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}