Chronoforms5 CURL target URL

scree 09 Apr, 2015
Hello, I have question for CURL function.

I am trying to send data to URL on background, but I did not manage to set up CURL function properly. When I am using the function REDIRECT in Chronoforms, everything works except the user is redirect to specific XML URL and I do not want this. That is why CURL is the best option because everything happens on background of the site.

My target URL is: https://xxx.hosting90.cz/api/domain_add_email?

and I am sending parametres:

sid=$result['sid']
email_subdomain_id="3229522"
name={username-register}
type="mailbox"
password={password-register}
weak="1"

The sid variable is something I am taking from XML response when I am log in to their API. I am using CURL for login on Load function, it gives me the sid variable by JSON response and I using it after in my custom code as:

<?php
$result = curl_exec($cURL);
$result = json_decode($result,true);
?>

Do I have to specify the variables in Target URL ? Because I still get error, that sic variable did not matched.

This is my debug code:

[Curl] => Array
(
[0] => CURL OK : the CURL function was found on this server.
[1] => $curl_values: email_subdomain_id=39522&name=demo789&type=mailbox&password=demo789&weak=1
[2] => curl_target_url: https://administrace.hosting90.cz/api/domain_add_email?
[3] => curl_errors:
[4] => curl_info: Array
(
[url] => https://administrace.hosting90.cz/api/domain_add_email?

And as I can see the sid varibale completely missing. Can you give me any tips where to start? Thank you
GreyHead 09 Apr, 2015
Hi scree,

A couple of things here.

+ The Target URL is the root part before the ? e.g. https://xxx.hosting90.cz/api/domain_add_email

+ Constant values require double quotes round the string

+ Form values just take the input name (or the $form->data name)

+ If you have other results you need to add them to the $form->data array e.g. $form->data['sid'] = json_decode($result,true);

So you would then have
sid=sid
email_subdomain_id="3229522"
name=username-register
type="mailbox"
password=password-register
weak="1"

Bob
scree 09 Apr, 2015
Hello GreyHead,

thank you for those informations, it is more clear now to me, how CURL function works and I did the neccessary modifications, you marked in the previous post.

The last thing drives me crazy, even when I use the targe URL just: https://xxx.hosting90.cz/api/domain_add_email

and I specify all the parametres below as:

email_subdomain_id="39522"
name=username-register
type="mailbox"
password=password-register
weak="1"

the debug mode gives me URL without those parametres, just do not put them in the final URL and I do not understand why. Have a look on debug.

[Curl] => Array
(
[0] => CURL OK : the CURL function was found on this server.
[1] => $curl_values: sid=22ul7jmo1mos75p50bi72hjg85&email_subdomain_id=39522&name=adasd&type=mailbox&password=sdada&weak=1
[2] => curl_target_url: https://administrace.hosting90.cz/api/domain_add_email
[3] => curl_errors:
[4] => curl_info: Array
(
[url] => https://administrace.hosting90.cz/api/domain_add_email
GreyHead 09 Apr, 2015
Hi scree,

That all looks OK - the cURL is sent using POST so the values don't need to be in the URL. The query string seems to me to be complete
sid=22ul7jmo1mos75p50bi72hjg85&email_subdomain_id=39522&name=adasd&type=mailbox&password=sdada&weak=1


Bob
scree 10 Apr, 2015
Hello GreyHead again,

last help with CURL required, I still have a problem to send the data on background of the Chronoforms. I am 100% percent sure that the target URL is alright to create new account by API CURL. Here what I am using in CURL function.

curl_target_url: https://xxx.hosting90.cz/api/domain_add_email
$curl_values: sid=6sln2kljim9pmfi745f7lvraq7&email_subdomain_id=39522&name=werwe&type=mailbox&password=wr&weak=1

This is what I am sending by CURL and it gives me back the error: <?xml version="1.0" encoding="UTF-8"?>
<reply><status><code>3</code><text>Bad sid</text></status></reply>

But I am sure the SID number is alright, because if I paste this full URL request to my webbrowser as follow:
https://xxx.hosting90.cz/api/domain_add_email?sid=6sln2kljim9pmfi745f7lvraq7&email_subdomain_id=39522&name=werwe&type=mailbox&password=wr&weak=1

It pass and it is OK and I have a message that request was OK and new account was established. So I really cannot see the difference when I use the Target URL and parametres, but one method (CURL) gives me an error and other method (direct URL or REDIRECT) pass the data in a right way.

And last weird thing with CURL I found in Chrono5, when I login to my private account (to obtain the actuall SID number by JSON), I use the function CURL onLoad form and I cannot do it the way you wrote me with "fieldsMap" and "targetURL" separately, it just do not pass the login. I have to write the full targetURL to be logged as follows: https://xxx.hosting90.cz/api/login?uid=121576&password=bomboclaat23&reply=json (when I dividing it - it returns an error that login credential are not right).
GreyHead 10 Apr, 2015
Answer
HI scree,

I'm guessing here but does the service you are using accept POST ? I suspect that the default CURLOPTs that CF is using somehow don't meet the requirements of the service you are working with.

You can add a cURL call in a Custom Code action with different CURLOPTs if necessary.

Bob
scree 10 Apr, 2015
Hi GreyHead thank you for clear informations, I think you are guessing right. I just looked to an API documentation from my provider and this function using only GET method. So the CURL plugin in CF5 use the POST, I get it now.
Now, I will try to use the PHP custom code to get this CURL function work properly.
-----------------------------------------------------------------------------------------------------------------------------------------------------------

I have only last question similiar with that issue, I need to obtain JSON output variable to an array, this is what I get as an JSON output when I login by CURL into my account: (I can have XML output as well, but JSON is maybe more secure)

{"reply":{"status":{"code":0,"text":"You are logged in"},"uid":"121576","sid":"s20bl56t2fv120gg4prcuh4qv2"}}
[sid] =>

I need only re-use this "sid" variable in my array, so I can use it later to my CURL API call. What I actually have in custom code is this:

<?php
$form->data['sid'] = json_decode($reply, true);
?>

But as you can see in debug mode, it gives me back the empty value. I am using this custom code by onload not on submit form.

Can you give me a tip how to get this variable from JSON format to PHP onLoad? Thanks a lot.
GreyHead 10 Apr, 2015
HI scree,

Here's a code snippet that should work. The first part unpacks, the JSON string; the next uses Joomla! methods to build the URL; the last part is a cURL function that actually sends the query. The two commented lines can be un-commented fro debugging.
<?php

$reply = '{"reply":{"status":{"code":0,"text":"You are logged in"},"uid":"121576","sid":"s20bl56t2fv120gg4prcuh4qv2"}}';
$reply = json_decode($reply);
$sid = $reply->reply->sid;

$url = new JURI('https://administrace.hosting90.cz/api/domain_add_email');
$url->setVar('sid', $sid);
$url->setVar('email_subdomain_id', '39522');
$url->setVar('name', $form->data['name']);
$url->setVar('password', $form->data['password']);
$url->setVar('type', 'mailbox');
$url->setVar('weak', '1');
$url_string = $url->toString();
doCurl($url_string);

/**
 * Makes the cURL call to the server
 */
function doCurl($url = null) {
  if ( empty($url)) {
    return false;
  }
  // echo'<div>$url: '.print_r($url, true).'</div>';
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_POST, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $output = curl_exec($ch);
  // echo'<div>$output: '.print_r($output, true).'</div>';
  curl_close($ch);
}
?>

Bob
scree 13 Apr, 2015
Yes GreyHead, your code works like a charm as always, thank you very much for help.

Just one last thing is on my mind. I need to load this variable "sid" from JSON output. It is a variable, it changes every 20 minutes, so I need to load the actuall data from "sid". On form load I have the following array of JSON output:

Array
(
[curl] => HTTP/1.1 200 OK
Server: nginx
Date: Mon, 13 Apr 2015 06:21:57 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: PHPSESSID=2glgqljrun6edf33111f1j6sg7; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: logged_in=1; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/
Set-Cookie: logged_in=1; path=/
Access-Control-Allow-Origin: *
Strict-Transport-Security: max-age=31536000

{"reply":{"status":{"code":0,"text":"You are logged in"},"uid":"121576","sid":"2glgqljrun6edf33111f1j6sg7"}}
)

Do you know, how I can re-take the varible "sid" from this JSON reply status? And how can I re-send the CURL with actuall "sid"?
I cannot use this definition: $reply = '{"reply":{"status":{"code":0,"text":"You are logged in"},"uid":"121576","sid":"s20bl56t2fv120gg4prcuh4qv2"}}';

I need to load the "sid" from JSON output. Thank you for the last help.
GreyHead 13 Apr, 2015
Hi scree,

Sorry I used that line for testing and forgot to remove it - replace it with the code that you use to get the sid result.

Bob
scree 13 Apr, 2015
Hello GreyHead, I am really stucked on this problem again, I do not know how I can save the JSON data output to an array and re-use it in submit form. I tried almost everything, but I could not recieve this variable from JSON output and save it to array:

On form load debug I have (those JSON output come from CURL on form load):

Array
(
[curl] => {"reply":{"status":{"code":0,"text":"You are logged in"},"uid":"121576","sid":"uf2bet93925a61jk1b4td1n681"}}
)

Than I try almost everything with custom code to load this SID variable to my array. I have the following code plus proche.

$reply = $curl(array);
$reply = json_decode($reply);
$sid = $reply->reply->sid;
echo'<div>$url: '.print_r($sid, true).'</div>';

Gives me nothing, could you help me how to load this variable? Than it is ok I hope:) nothing left to finish my APi call. Thank you for any tip.
GreyHead 13 Apr, 2015
HI scree,

Why do you have the cURL on the form load if you don't need it until the submit?

If that is the only way put the value of the sid into $form->data['sid']; add a hidden input to the form with the name sid; then the value will be available as $form->data['sid'] when the form submits.

Bob
scree 14 Apr, 2015
1 Likes
Hello,

yes finally I get it done thanks to all your tips and help I really appreciate and I want to write some final steps I made.

I need the function CURL on load, because I use CURL connection to login to API with my credentials and when the login is OK, the JSON gives me back the output response with variable SID. This varible serves me for other API call I make by CURL later, to authorize my final API request.

So I put the CURL option on form load, simply to connect to my API with credentials (username and password). This gives me a JSON response which is saved to array on form load. The varible named $curl gives by JSON is:

[curl] => {"reply":{"status":{"code":0,"text":"You are logged in"},"uid":"121576","sid":"uf2bet93925a61jk1b4td1n681"}}

This is till on my form load.

All I need is the SID number to authorize my next request on form submit. So I make a hidden output called "curl" in my form. When the input is not hidden, I can see the data it takes from JSON (later I can make a hidden field), so than I parse it by custom code:

$reply = $form->data['curl']; // takes JSON output from hidden field
$reply = json_decode($reply); // decode it
$sid = $reply->reply->sid; // take varible SID I am intersted in

So now I have the varible $SID which is the number I am interested in and I can send it forward, by CURL GET method posted by GreyHead few boxes above.

So it is really simple, I am happy that is done. Thank you and hope that someone can profit from this post.
scree 20 Apr, 2015
Hi, I´am using CURL custom code GreyHead indicated here, few posts back to obtain the GET CURL method, because CH5 CURL function using the POST methiod. Everything goes fine so far.

I have now the varibale $output, which gives me back the JSON reponse as:

$output = {"reply":{"status":{"code":0,"text":"OK"},"emails":[{"email_id":"104036","name":"admin","full_email":"admin@privatemail.cz","type":"mailbox","redirect":"","quota":{"usage":"0,02","limit":"1111,11"}},

I can test it, I can print it as follows: echo'<div>$output = '.print_r($output, true).'</div>';

But I cannot save it to array? I am using: $form->data['newid'] = $output just after the "curl_close($ch);"
I created the input field: "newid", I am using CURL onLoad, but the JSON response is not showing in input filed.

It´s a pitty because I would like to re-use the variable later and if I cannot save it to array, this variable serves me just for simple custom code.

Can you help me, how to save it to array and use this variable later? Thank you for any help.
GreyHead 20 Apr, 2015
Hi scree,

You can convert it to an array with
$output = json_decode($output, false);
or to a PHP object with
$output = json_decode($output);

Bob
This topic is locked and no more replies can be posted.