I'm trying to send data from Salesforce to an endpoint hosted on an external server. The endpoint is written in PHP that will insert a new record into an external MySQL database.

I've got the connections setup (I.E. I can trigger my apex class that makes the callout and a dummy record is inserted (hard-coded values)).

I'm struggling to actually send dynamic values in my request body and use the values on the other side.

I tried following this guide Salesforce callout to update external MySQL database and have modified it for my use case (below) but I've gotten nowhere.

HttpRequest req = new HttpRequest();
req.setEndpoint('removed for security');

//Create new object to pass through as JSON
Error_Log__c error = new Error_Log__c();
error.process__c = 'Flow';

String JSONString = JSON.serialize(error);            


Http http = new Http();
HTTPResponse res = http.send(req);

And my PHP code is below

$myObjectJSON = $_POST['Error_Log__c'];
$myObject = json_decode($json);
$pp = $myObject->Process__c;
  • Please edit your question to show us what you've tried so far.
    – Derek F
    Mar 12, 2023 at 13:53
  • @DerekF My question has been modified Mar 12, 2023 at 14:53

1 Answer 1


Using the $_POST superglobal as an array in PHP is more for when you have x-form-www-urlencoded data in the request body, not so much when you're sending JSON.

i.e. that's how you would pull data out if you were sending it like var1=value1&var2=value2 ($_POST['var1'])

Since you are working with JSON, you need to get the entirety of the request body and pass it to json_decode(). Some brief searching suggests that you should use the php://input i/o stream for this.

// The php://input stream can't be used directly
// Gotta use something that can buffer the entirety of the stream 
$postBody = file_get_contents('php://input');

// After which you can decode
// Seems to give you an associative array by default (though you can
//   set a flag or pass an argument to get an object instead)
$data = json_decode($postBody);

// Keep in mind that PHP variables are case-sensitive (unlike Apex, which is definitely
//   the odd duck in the larger family of programming languages)
// Check how your JSONString is being serialized in Apex, you will need to 
//   use the same capitalization in PHP
$process = $data['process__c'];
  • Hi @DerekF, that makes sense - I appreciate the comment. When I try to echo the $process it returns no value, but if I loop through data and echo each item then I get the value. Do you know why echoing the value $process alone does not return anything? (This is using the code you helpfully provided) Mar 12, 2023 at 15:21
  • @joshholloway like I said, be sure that the capitalization is correct.
    – Derek F
    Mar 12, 2023 at 15:22
  • My code below is pretty much what you had, plus the echo, letter for letter in regards to capitals .$postBody = file_get_contents('php://input'); $data = json_decode($postBody); $process = $data->process__c; echo "\nPROCESS", $process, "\n"; Mar 12, 2023 at 15:28
  • @joshholloway I mean that the capitalization is correct for the data coming from your org. I had to take a guess at what the capitalization should be. Do a system.debug() in your Apex to check. I.e. if it's PROCESS__c, then you'd need to do do $data->PROCESS__c in php (and not $data->process__c. Again, because php variables are case-sensitive).
    – Derek F
    Mar 12, 2023 at 15:43
  • Hi, you got it correct first time around, it's lowercase coming out of the org, I've even do a manual JSON object with 'gen.writeStartObject()' and I'm still getting the same issue. i cannot reference the field itself but I can loop through the object and get all of the fields & values. Mar 12, 2023 at 15:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .