I am trying to call a PHP post api in Golang using HTTP package with some post values. After sending these parameters from Golang I am trying to get it into PHP $_POST array in Wordpress. But I am not able to access any index of $_POST array in PHP code.
Here is my code:
func SaveProviderInGlobalDb(providerId int, providerEmail string, businessName string) (*http.Response, error){
postValues := url.Values{}
providerIdString := strconv.Itoa(providerId)
postValues.Add("provider_id", providerIdString)
postValues.Add("provider_email", providerEmail)
postValues.Add("business_name", businessName)
response, err := http.PostForm("http://localhost/provider.php", postValues)
return response, err
}
PHP code:
if(isset($_POST) && count($_POST) > 0){
if(isset($_POST["business_name"]) && $_POST["business_name"] != ''){
$providersCount = GetProviderCount($_POST["business_name"], $_POST["provider_email"]);
if($providersCount > 0) {
$response["response"] = "This provider is already registered for ".$_POST["business_name"];
$response["code"] = 400;
}else{
$result = SaveProvider($_POST);
if($result){
$response["response"] = "Provider saved successfully.";
$response["code"] = 200;
}else{
$response["response"] = "Error while saving provider.";
$response["code"] = 400;
}
}
}else {
$response["response"] = "No merchant found.";
$response["code"] = 400;
}
echo json_encode($response);
}
In the above PHP code I am not able to check if "business_name" is set in $_POST or not. Due to this, It always gives me error message that "No merchant found."
EDITED
Array
(
[------WebKitFormBoundaryGPuoAolbEsWBZnVh
Content-Disposition:_form-data;_name] => \"provider_email\"
john_doe_1234@example.com
------WebKitFormBoundaryGPuoAolbEsWBZnVh
Content-Disposition: form-data; name=\"provider_id\"
54
------WebKitFormBoundaryGPuoAolbEsWBZnVh
Content-Disposition: form-data; name=\"business_name\"
testkom1
------WebKitFormBoundaryGPuoAolbEsWBZnVh--
)
It is the response which I am getting after printing $_POST.
Can anyone tell me how do I solve this problem ?
Thanks!
Change your condition statement:
if(isset($_POST["business_name"]) && $_POST["business_name"] != '')
to:
if(!empty($_POST["business_name"]))
I hope it could do the job.
Actually I have accidentally passed x-WWW-form-urlencoded headers in post request. Due to this it was generating output like above. When I removed this header, It worked fine.
Thanks!