that's my php function that connect to a webservice where I'm passing static data to recive a result back:
function calculate(){
if(isset($_POST['calculate-button'])){
session_start();
$ch = curl_init();
$input1 = '{"input": {
"IDCompany": "CLM",
"IDLanguage": "EN",
"IDWsType": "1",
"Item_Input": 6,
"Input_Parm": [
{
"type_param" : "char",
"lenght_param" : 3,
"param" : "004"
},
{ "tipo_param" : "num",
"lenght_param" : 10.0,
"param" : "150"
},
{ "tipo_param" : "num",
"lenght_param" : 10.0,
"param" : "250"
},
{
"tipo_param" : "num",
"lenght_param" : 10.0,
"param" : "10"
},
{
"tipo_param" : "num",
"lenght_param" : 10.0,
"param" : "5"
},
{
"tipo_param" : "num",
"lenght_param" : 10.0,
"param" : "260"
}
]
}
}';
$headers = array();
$input2 = array('code' => '000001');
$headers[] = 'Content-Type:application/json';
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch,CURLOPT_URL, 'https://urltowebservice');
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch,CURLOPT_POSTFIELDS, $input1);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 60); // Timeout in seconds;
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
var_dump($ch);
}
curl_close($ch);
echo var_dump(json_decode($result, true));
}
This display correctly the results form webservice. It repsponds with a number and the product that should be purchased. What i'm trying to do now is to pass variables coming from a php form:
function calculate(){
if(isset($_POST['calculate-button'])){
session_start();
$ch = curl_init();
// variable from html form
$product = $_POST['product'];
$lenght = $_POST['lenght'];
$width = $_POST['width'];
$depth = $_POST['depth'];
$perimeter = $_POST['perimeter'];
$area = $_POST['area'];
$input1 = '{
"input": {
"IDCompany": "CLM",
"IDLanguage": "EN",
"IDWsType": "1",
"Item_Input": 6,
"Input_Parm": [
{ "type_param" : "char",
"lenght_param" : 3,
"param" : "$product"
},
{
"tipo_param" : "num",
"lenght_param" : 10.0,
"param" : "$lenght"
},
{
"tipo_param" : "num",
"lenght_param" : 10.0,
"param" : "$width"
},
{
"tipo_param" : "num",
"lenght_param" : 10.0,
"param" : "$depth"
},
{
"tipo_param" : "num",
"lenght_param" : 10.0,
"param" : "$perimeter"
},
{
"tipo_param" : "num",
"lenght_param" : 10.0,
"param" : "$area"
}
]
}
}';
$headers = array();
$input2 = array('code' => '000001');
$headers[] = 'Content-Type:application/json';
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch,CURLOPT_URL, 'https://urltowebservice');
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch,CURLOPT_POSTFIELDS, $input1);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 60); // Timeout in seconds;
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
var_dump($ch);
}
curl_close($ch);
echo var_dump(json_decode($result, true));
}
}
But i've got NULL as result. If I try to echo all the variable I can see that the form sends them correctly. The error is probably related to how to send these to the webservice. Any idea to solve this? Many thanks
CodePudding user response:
$variables are not expanded when inside a single quoted literal, it has to be a double quoted literal.
So for example change like this
"param" : "' . $product . '"
and so on for all the values
CodePudding user response:
I believe I see the problem. In PHP text inside of single quotes is sent as is. So you are passing the string $product
. To avoid a lot of string delimiter switching, try something like this:
{ "type_param" : "char",
"lenght_param" : 3,
"param" : "'.$product.'"
},
This will concatenate the value from the variable into the json string you are building. You will want to do this for every variable you use.
TL;DR: Use a .
to join strings together.