Lads, I have 2 PHP script (A and B) running as Microservices on different subsomains. Script B receives data from whoever is posting it as follow:
<?php
error_reporting(1);
header("Access-Control-Allow-Origin:*");
//Response Array
$res = array();
require("Integration.php");
$data = json_decode(file_get_contents('php://input'));
if(logToDatabase($data)){
echo "Success";
}else{
echo "Failed";
}
Whereas Script A is supposed to be Posting the Data as follows:
<?php
$data = array(
"creditor"=>"+265994791131",
"recipient"=>"+265994791131",
"amount"=>"500",
"SECRET_BADGE"=>"12345"
);
$response = sendPost($data);
echo $response;
function sendPost($data) {
echo "Initializing Post...<br>";
try{
$ch = curl_init();
$endpoint = "https://sub.domain.com/B.php";
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
echo json_encode($data);
//Set your auth headers
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-type: application/x-www-form-urlencoded"
));
echo "Executing Curl...<br><br>";
$result = curl_exec ($ch);
curl_close ($ch);
return $result;
}catch(Exception $e){
echo "Error with Request!!";
return $e;
}
}
My problem is when I Post the data like this, it doesn't seem to be received on the end because it doesn't get logged in the database as I am doing.
To narrow down the source of the problem. I wrote another script in Node which just post the same data to the B.php
script using node-fetch
and it works as below:
const fetch = require("node-fetch")
console.log("Test script started...")
const data = {
"creditor":"+265994791131",
"recipient":"+265994791131",
"amount":400,
"SECRET_BADGE":"Atyeiu36896893hn"
}
const options = {
method:"POST",
headers:{
"Content-Type":"application/x-www-form-urlencoded;utf-8"
},
body:JSON.stringify(data)
}
let HttpPost = async ()=>{
try{
let response = await fetch("https://sub.domain.com/B.php", options)
response = await response.json()
console.log(JSON.stringify(response))
}catch(e){
console.log(e)
}
}
HttpPost()
@TangentiallyPerpendicular, this is what I get when I dump curl_getinfo($ch)
:
"url":"https:\/\/sub\/domain.com",
"content_type":null,
"http_code":0,
"header_size":0,
"request_size":0,
"filetime":-1,
"ssl_verify_result":0,
"redirect_count":0,
"total_time":2.036406,
"namelookup_time":0,
"connect_time":0,
"pretransfer_time":0,
"size_upload":0,
"size_download":0,
"speed_download":0,
"speed_upload":0,
"download_content_length":-1,
"upload_content_length":-1,
"starttransfer_time":0,
"redirect_time":0,
"redirect_url":"",
"primary_ip":"",
"certinfo":[
],
"primary_port":0,
"local_ip":"",
"local_port":0,
"http_version":0,
"protocol":0,
"ssl_verifyresult":0,
"scheme":"",
"appconnect_time_us":0,
"connect_time_us":0,
"namelookup_time_us":0,
"pretransfer_time_us":0,
"redirect_time_us":0,
"starttransfer_time_us":0,
"total_time_us":2036406
}
This works flawlessly, but I really need to do this using PHP. What am I doing wrong? I considered using a PHP Http Client library e.g Guzzle
, but I am facing issue with composer permission to open I/O streams, but that a totally different issue for another question.