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

JSON call - PHP to CFML translation

Participant ,
Jul 27, 2010 Jul 27, 2010

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
1.6K
Translate
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 ...?

Translate
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 ...?

Translate
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

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...

Translate
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

Try using cfhttpparam type="body"

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

Translate
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

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":[]}]}

Translate
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

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".

Translate
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

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

Translate
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
<cfhttpparam type="body" name="params" value="#jsondata#">

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

Translate
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

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!

Translate
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
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)>

Translate
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
LATEST

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

Translate
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