Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Send data to a web api Topic is solved

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

Send data to a web api

Unread post by yvesf »

Hello,

I am trying to send data to a web api. By doing so, I have created button into my edit form to send this data. This button invokes a procedure which is doing the put request to the related api.
Capture.PNG
If I am doing this update directly in command line with curl, it works.
Curl.PNG
Here is the procedure

Code: Select all

$params=['mesures'=>'updated']; // assigning updated to mesures fields
//$params=['mesures=updated'];
$defaults = array(
CURLOPT_URL => 'http://localhost/api/mesures/1',
CURLOPT_PUT => true,
CURLOPT_POSTFIELDS => http_build_query($params),
);
$ch = curl_init();
curl_setopt_array($ch, ($defaults));
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close ($ch);

I have a fatal error as following :
FatalError.PNG
Any idea ? Is there a mean to debug step by step this kind of pb ? Many thanks
You do not have the required permissions to view the files attached to this post.
kev1n
nuBuilder Team
Posts: 4297
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Send data to a web api

Unread post by kev1n »

Try setting some options after curl_init()

Code: Select all

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
Or try something as shown here:

https://gist.github.com/timw4mail/1315076
yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

Re: Send data to a web api

Unread post by yvesf »

I have found a website generating the php code corresponding to the request you would like to push. I have work with their example to ensure not doing something wrong.
https://reqbin.com/req/3mrxjgw4/post-xml-example
I have taken that and put it in a procedure called via a button on an edit form.

Code: Select all

$url = "https://reqbin.com/echo/post/xml";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
   "Content-Type: application/xml",
   "Accept: application/xml",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$data = <<<DATA
<?xml version="1.0" encoding="utf-8"?>
<Request>
    <Login>login</Login>
    <Password>password</Password>
</Request>
DATA;

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($curl);
curl_close($curl);
It doesn't work... No action on the related api.
Anyone has an idea ?
kev1n
nuBuilder Team
Posts: 4297
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Send data to a web api

Unread post by kev1n »

Do you get an error when adding this code after curl_exec() ?

Code: Select all

$error = curl_error($curls);
nuDebug($error);
However, most likely, this is not a nuBuilder specific issue.
I recommend asking these kind of question at https://stackoverflow.com. There are a lot of experts.
yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

Re: Send data to a web api

Unread post by yvesf »

I have found the solution.

Code: Select all

$url = "http://localhost/api/mesures/1";
$data = "mesures=test2";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
Note : $data should not be converted to json in that case. I don't know why. 2 days of search. If it could help anyone...
Post Reply