0

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.

CliffTheCoder
  • 394
  • 1
  • 4
  • 24
  • Add a call to [curl_getinfo()](https://www.php.net/manual/en/function.curl-getinfo.php) after you've sent the request but before you close the session. See what that tells you about the transfer – Tangentially Perpendicular Apr 13 '21 at 04:47
  • Do you want to post JSON or `application/x-www-form-urlencoded`? `B.php` seems to expect JSON. Your JS code posts JSON (even though you've got the wrong `Content-type` header). But your PHP code is posting `application/x-www-form-urlencoded` – Phil Apr 13 '21 at 05:05
  • @TangentiallyPerpendicular, I have updated the question to include the ```curl_getinfo()``` response. More curious as to why it say ```Content-type:null``` when I have clearly set it – CliffTheCoder Apr 13 '21 at 05:12
  • I've closed this with a link showing how to post JSON with PHP `curl`. Let me know if that doesn't solve your issue – Phil Apr 13 '21 at 05:13
  • @Phil, I have changed the content type to json, but still no change – CliffTheCoder Apr 13 '21 at 05:13
  • You also need to `json_encode()` the `$data` used for `CURLOPT_POSTFIELDS` – Phil Apr 13 '21 at 05:14
  • @Phil, I have updated the code accordingly but when I ```var_dump``` the curl execution result, I notice the content type is ```text/html``` regardless of what I set it to in the header – CliffTheCoder Apr 13 '21 at 05:26
  • That would be the **response** content-type (from `B.php`), not the **request** one – Phil Apr 13 '21 at 05:58
  • Correction, I mean the ```curl_getinfo()``` response – CliffTheCoder Apr 13 '21 at 07:02

0 Answers0