The Salesforce Developers website will undergo maintenance on May 29, 2024 from 3:00 a.m. UTC to 10:00 a.m. UTC. The maintenance process may affect the availability of our documentation. Please plan accordingly.

I tweeted last week that, in the course of answering a discussion board post, I’d fixed a couple of minor issues in the Toolkit for PHP that had made it impossible to use the Metadata API to add custom fields to an object from PHP. Although my fix worked, I noticed that only the CustomObject and CustomField metadata types are supported by the PHP toolkit; it’s not possible to, for example, set the Login IP Range on a Profile from PHP. John Casimiro suggested taking a look at the WSO2 Web Services Framework for PHP (aka WSF/PHP) wsdl2php tool to generate PHP classes from the Metadata API WSDL.

Generating the classes was very straightforward – I downloaded and extracted WSF/PHP, generated and downloaded the current Metadata API WSDL from my org, and ran the wsdl2php script:

This gave me PHP classes corresponding to the types in the WSDL, plus some sample code showing how I could use those types with WSF/PHP’s WSClient. Since I wanted to use the existing PHP toolkit to manage the connection with the server, I just trimmed off the sample code (everything from the $class_map on) and added a PHP namespace at the top. Here is the resulting metadata.php.

With a couple more tweaks to the PHP toolkit, I was able to create, update and delete any metadata types from PHP. Here is my script to set Login IP Ranges on a Profile:

I haven’t decided yet whether to include metadata.php in the toolkit itself, just document the process to create it from the current Metadata WSDL, or both. I’d be interested in feedback from the community on this, so please leave your comments!

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS