• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

JSON call - PHP to CFML translation

Participant ,
Jul 27, 2010 Jul 27, 2010

Copy link to clipboard

Copied

Hi all,

I am working on communicating to an api of a domain register, now i found the following code that is voor php:

$domain = "test.nl";
$params = array();
$request_url = "https://httpapi.yoursrs.com/v1/domains/" . urlencode($domain) . "/check";
$response = sendRequest($request_url, $params, "tester", "pass1234");
print_r($response);

Now i need to change this to cfml code and i tried:

<cfset jsondata = {
login_handle = "username",
login_pass = "password"
} />

<cfhttp
url="https://httpapi.yoursrs.com/v1/domains/test.nl/check"
method="post"
result="result">

#SerializeJSON(jsondata)#  

</cfhttp>

Still i have no luck, does anyone can translate that php to cfml?

TOPICS
Advanced techniques

Views

1.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Valorous Hero , Jul 28, 2010 Jul 28, 2010

$response = sendRequest($request_url, $params, "tester",

"pass1234");

A quick google search suggests sendRequest is not a standard php function. What does the sendRequest function do and how is json involved ...?

Votes

Translate

Translate
Valorous Hero ,
Jul 28, 2010 Jul 28, 2010

Copy link to clipboard

Copied

$response = sendRequest($request_url, $params, "tester",

"pass1234");

A quick google search suggests sendRequest is not a standard php function. What does the sendRequest function do and how is json involved ...?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jul 28, 2010 Jul 28, 2010

Copy link to clipboard

Copied

From the API doc this is the code how it should be used:

Request


Method: POST
URL: https://httpapi.yoursrs.com/v1/domains/example.com/check
BODY:
{
"login_handle":"example_user",
"login_pass":"********"
}


Response


{
"command":"domain:check",
"handle":"example.com",
"code":1000,
"msg":"Command completed successfully",
"error":[
],
"params":{
"handle":"example.com"
},
"svTRID":"158759-2-yoursrs",
"response":{
"avail":"0"
}
}

So this JSON markup needs to be placed inside the body of the post request.

Any idea's? Or examples somewhere?

I can find pieces of info around the net but nowhere a full example...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jul 28, 2010 Jul 28, 2010

Copy link to clipboard

Copied

Try using cfhttpparam type="body"

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_g-h_10.html

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jul 28, 2010 Jul 28, 2010

Copy link to clipboard

Copied

Tried that also like:

<cfhttp url="https://httpapi.yoursrs.com/v1/domains/test.nl/check" method="post">

<cfhttpparam type="body" name="login_handle" value="namevalue">
<cfhttpparam type="body" name="login_pass" value="passvalue">

</cfhttp>

#cfhttp.fileContent#

Keep getting error about:

{"command":null,"handle":null,"code":2400,"msg":"Command failed","error":["Internal Server Error. resulted in the following error: array_key_exists() [function.array-key-exists<\/a>]: The second argument should be either an array or an object"],"params":[],"svTRID":null,"response":[],"children":[{"command":"Internal Server Error.","handle":null,"code":2400,"msg":"Command failed","error":["array_key_exists() [function.array-key-exists<\/a>]: The second argument should be either an array or an object"],"params":{"errno":2,"errstr":"array_key_exists() [function.array-key-exists<\/a>]: The second argument should be either an array or an object","errfile":"\/home\/live\/httpapi\/v1\/index.php","errline":54},"svTRID":null,"response":[]}]}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jul 28, 2010 Jul 28, 2010

Copy link to clipboard

Copied

From what you have described, the encoded json should be sent as the body of the request. So there should be only one tag with type="body".

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jul 28, 2010 Jul 28, 2010

Copy link to clipboard

Copied

OK, tried this:

<cfset jsondata = {
login_handle = "namevalue",
login_pass = "passvalue"
} />

<cfset jsondata = SerializeJSON(jsondata)>

<cfhttp
url="https://httpapi.yoursrs.com/v1/domains/test.nl/check"
method="post">

<cfhttpparam type="body" name="params" value="#jsondata#">

</cfhttp>

Now i get:

{"command":null,"handle":null,"code":2003,"msg":"Required parameter  missing","error":["Parameter 'login_handle' missing.","Parameter  'login_pass'  missing."],"params":{"LOGIN_PASS":"passvalue","LOGIN_HANDLE":"namevalue","handle":"test.nl"},"svTRID":null,"response":[]}

Message was edited by: marco van den oever

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jul 28, 2010 Jul 28, 2010

Copy link to clipboard

Copied

<cfhttpparam type="body" name="params" value="#jsondata#">

It looks right.  Try it without the name "params". Maybe that is confusing things?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jul 28, 2010 Jul 28, 2010

Copy link to clipboard

Copied

Ah got it, i used:

<cfset jsondata = {
login_handle = "namevalue",
login_pass = "passvalue"
} />

That should be:

<cfset jsondata = {
"login_handle":"namevalue",
"login_pass":"passvalue"
} />

So the JSON markup was not correctly set...

Thanks for correcting and guiding me!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jul 28, 2010 Jul 28, 2010

Copy link to clipboard

Copied

So the JSON markup was not correctly set...

Yes, but calling SerializeJSON() before you send the packet should generate the correct mark up.  Maybe it was a case sensitivity problem.  (Notice how CF converted the keys to upper case in the error message). Just for grins try setting the keys with assocative array notation.  Any difference ?

<cfset jsonData = {}>

<cfset jsonData["login_handle"] = "namevalue">

<cfset jsonData["login_pass "] = "passvalue">

<cfset jsonData = SerializeJSON(jsonData)>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jul 28, 2010 Jul 28, 2010

Copy link to clipboard

Copied

LATEST

I tried and now it works! That is the correct way of doing so

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation