1

Can anyone tell me how to convert this line of PHP to ColdFusion?

$dev_name = "xxx";
$cert_name = "yyy";
$url = "https://xxxx.com/";
$headers = array("X-BONANZLE-API-DEV-NAME: " . $dev_name 
                  , "X-BONANZLE-API-CERT-NAME: " . $cert_name);

Which is an array. I have tried this but it fails.

<cfset dev_name="xxx">
<cfset cert_name ="yyy">
<cfset headers = {X-BONANZLE-API-DEV-NAME:"#dev_name#"
                    , X-BONANZLE-API-CERT-NAME:"#cert_name#"}>

<cfdump var="#headers#">

</cfdump>

1 Answer 1

2

You need to quote the key names also same as you did in the PHP version.

The issue that you are hitting is that your key names contain a "minus" character. To workaround this issue you need to quote your keyname.

Also in your sample CFML code you will end up creating a struct, it looks to me what you want is an array of structs.

So something like this should get you what you want:

<cfset headers = [ {"X-BONANZLE-API-DEV-NAME":"#dev_name#"}
                   , {"X-BONANZLE-API-CERT-NAME":"#cert_name#"} ]>

or if you just want an array of strings:

<cfset headers = [ "X-BONANZLE-API-DEV-NAME:" & dev_name
                   ,"X-BONANZLE-API-CERT-NAME:" & cert_name ]>

Note: the square brackets which indicate that you want an array and the curly braces indicate you want a struct. It's very similar to JSON notation.

If you are on an old version of coldfusion you would need to do something like this:

For a array of structs:

<cfset headers = arrayNew(1)>
<cfset headers[1] = structNew()>
<cfset headers[1]["X-BONANZLE-API-DEV-NAME"] = dev_name>
<cfset headers[2] = structNew()>
<cfset headers[2]["X-BONANZLE-API-CERT-NAME"] = cert_name>

OR

For a array of strings:

<cfset headers = arrayNew(1)>
<cfset headers[1] = "X-BONANZLE-API-DEV-NAME:" & dev_name>
<cfset headers[2] = "X-BONANZLE-API-CERT-NAME:" & cert_name>
Sign up to request clarification or add additional context in comments.

5 Comments

is that only some of the function will only run in version 8 & above? because i have tried in version 7 that will pop me out an error.
i will post you an example when i am not on my iphone and on a proper machine.
OK take you time ..Thanks
I updated my answer with an example for old versions of coldfusion. Not sure if you are aware but if licensing cost are holding you back from upgrading it may be worth considering the open source Railo Sever as upgrade option. CFML 7 is pretty ancient.
yup i notice that. Thanks for helping M.Scherzer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.