ColdFusion9(CF9 public beta build) can now expose many of its features as document/literal style SOAP web services or AMF(Flash remoting). Users will be able to leverage ColdFusion functionality using web services from other languages like PHP/.NET/Flex etc. I will be explaining how user can call CF services from PHP or .NET using SOAP webservices.
Currently users can access following cf tags as services:
• cfchart
• cfdocument
• cfimage
• cfmail
• cfpop
• cfpdf
Enabling ColdFusion Services:
You must explicitly enable access to ColdFusion services. CF admin can limit access to specific URLs and can create multiple users with access to different subsets of the available service.Therefore, for example, you can allow an internal application access to all services, but limit the access of another application to image and charting services.
To enable service access:
1. On the CF Administrator Security –> Allowed IP Addresses page specify the IP addresses that can access the services. You can specify individual addresses, address ranges of the form 10-30, or * wild cards. You can specify IPv4 or IPv6 addresses. For example, you could use the following address patterns:
10.*.*.*
10*.30-50.20-30
10:10:10:10:10:10:10-FF:*
2. On the Administrator Security –> User Manager page, specify the users that can access the services. You must specify the user name and password. Also specify the allowed services in the Exposed Services section of the page, by default all the services are prohibited.
Accessing ColdFusion Services using SOAP:
You use SOAP to access ColdFusion services as document/style web services. To see the available service requests and responses, view the service WSDL in the CFWebRoot\CFIDE\services directory. For example, to see the image services, use a URL : http://cfserver:port/CFIDE/services/image.cfc?wsdl.
All the methods and attributes exposed through services can be found in wsdl. Attribute explanation is same as the attributes of corresponding ColdFusion TAGS/Functions.
Workflow for CF Web Service:
1) Enable access in ColdFusion administrator as mentioned in “Enabling ColdFusion Services” section
2) Upload the file to the server(for example, PHP), which calls CF web services.
3) Call CF web service with http source url pointing to uploaded file, service user name, password and other attributes.
4) ColdFusion server returns the response.
Accessing CF service from PHP:
1) Install PHP
2) Use a library or framework, which helps converting WSDL to PHP classes(wsdl2php). We tested with “WSO2 Web services Frame Work for PHP” (http://wso2.org/projects/wsf/php)
3) Invoke the class with required parameters.
The following example code section shows a portion of a PHP page that adds a border to an image:
//you should have classes generated by wsdl2php and required classmap here
// create client in WSDL mode
$client = new WSClient(array (“wsdl” =>”http://CFServer:Port/CFIDE/services/image.cfc?wsdl”,”classmap” => $class_map));
// get proxy object reference form client
$proxy = $client->getProxy();
$input = new AddBorder();
//Fill in the class fields of $input to match your business logic
$input->serviceusername = “myuser”;
$input->servicepassword = “mypassword”;
$input->url = “http://myPHPSite/Images/image.jpg”;
$input->thickness = “30″;
$input->color = “blue”;
$input->bordertype = “”;
// call the operation
$response = $proxy->AddBorder($input);
In the above code:
serviceusername –>The userName set in the ColdFusion admin with the permission to access the specific service being requested. (at Administrator Security –> User Manager page)
servicepassword –> Password set for the serviceusername.
url –> Image present in PHP server.
Other attributes are same as the ImageAddborder() ColdFusion function.
Batch operation on Image in PHP:
This method helps user to do multiple operations on uploaded image.The following code shows batchOperation(), which does Crop and AddBorder on an image.
$input = new batchOperation();
//Crop
$element1 = new Element();
$element2 = new Element();
$element3 = new Element();
$element4 = new Element();
$elementArray1 = new ArrayOf_xsd_anyType();
$element1->key = ‘x’;
$element1->value = ’10′;
$element2->key = ‘y’;
$element2->value = ’10′;
$element3->key = ‘width’;
$element3->value = ’200′;
$element4->key = ‘height’;
$element4->value = ’200′;
$elementArray1->item[0] = $element1;
$elementArray1->item[1] = $element2;
$elementArray1->item[2] = $element3;
$elementArray1->item[3] = $element4;
$ElementcollectionCrop = new Elementcollection();
$ElementcollectionCrop->key = ‘Crop’;
$ElementcollectionCrop->value = $elementArray1;
//AddBorder
$element5 = new Element();
$element6 = new Element();
$element7 = new Element();
$elementArray2 = new ArrayOf_xsd_anyType();
$element5->key = ‘thickness’;
$element5->value = ’30′;
$element6->key = ‘color’;
$element6->value = ‘green’;
$element7->key = ‘bordertype’;
$element7->value = ”;
$elementArray2->item[0] = $element5;
$elementArray2->item[1] = $element6;
$elementArray2->item[2] = $element7;
$ElementcollectionAddBorder = new Elementcollection();
$ElementcollectionAddBorder->key = ‘AddBorder’;
$ElementcollectionAddBorder->value = $elementArray2;
$input->serviceusername = “myuser”;
$input->servicepassword = “mypassword”;
$input->url = “http:/:/image.jpg”;
$input->attributes =
array($ElementcollectionCrop,$ElementcollectionAddBorder);
// call the operation
$response = $proxy->batchOperation($input);
response object is an URL of the modified image.
Calling CF Service from .NET:
1) Create a new Web service project in Visual Studio.
2) Add CF WSDL as a web reference to the project.
3) Add a new item(Say a Web Form)
using Document Service in .NET:
Following is the sample .NET code for creating pdf from www.google.com.
Document.DocumentService objWebService = new
Document.DocumentService();
Document.Documentsection[] docsectionArray = { };
Document.Documentitem[] docitemArray = { };
string result = objWebService.generate(“myuser”, “mypassword”, “pdf”,”", “”, “”, “”, “yes”, “”, “”, “”, “”, “”,”", “”, “”, “”, “”, “”, “”, “”, “”,”", “”, “”, “http://www.google.com/”, “”, “”, “”, “”, docsectionArray,docitemArray);
In the above code,
Document –> Web reference for document wsdl.
generate() –> method to generate pdf from html.
Let me know if you need more information.