Search code examples
phparraysrestcurlphp-curl

Parsing JSON for making a POST request on Salesforce platform


After updated the code, now I can see the error string(148) "[{"errorCode":"APEX_ERROR","message":"System.TypeException: Invalid conversion from runtime type List to Map\n\n(BMCServiceDesk)"}]"

Just to explain: I am creating an integration to open a Service Request in my salesforce instance (remedyforce application).

I created the file below, I've had several errors and corrected it until I didn't give any more errors, but now I can see only invalid conversion. Maybe because my json.

<?php 

$url = 'https://URL.my.salesforce.com/services/apexrest/BMCServiceDesk/1.0/ServiceRequest/';
$ch = curl_init($url);

$sf_auth = 'Bearer XXXXXX';

$params = array("[
  'Fields' => [
    0 => [
      'Name' => 'requestDefinitionId',
      'Value' => 'a3Hf0000000lTNaEAM',
    ],
    1 => [
      'Name' => 'client',
      'Value' => '0053j00000A7rWLAAZ',
    ],
  ],
  'Answers' => [
    0 => [
      'QuestionId' => 'a3Df0000000qI63EAE',
      'Values' => [
        0 => 'Reclamação',
      ],
    ],
    1 => [
      'QuestionId' => 'a3Df0000000qHvsEAE',
      'Values' => [
        0 => 'Solicitação de Serviço aberta por qualquer integração web',
      ],
    ],
    2 => [
      'QuestionId' => 'a3Df0000000qHwREAU',
      'Values' => [
        0 => 'Web',
      ],
    ],
  ],
]");
           
    $data_string = json_encode($params);
    
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Accept: application/json',
        'Content-type: application/json',
        'Authorization:' . $sf_auth,
    ));

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    //execute post
    $result = curl_exec($ch);
    
    //close connection
    curl_close($ch);
    
    $response = curl_exec($ch);
    
    echo curl_errno($ch) . '<br/>';
    echo curl_error($ch) . '<br/>';

    var_dump($result);
    
?>

Solution

  • After made the changes, it is ok now.

    So I'll leave this code if anyone that uses Remedyforce need a base to start this kind of integration.

    <?php 
        // Inicia
    $url = 'https://MYURL--trnmnto.my.salesforce.com/services/apexrest/BMCServiceDesk/1.0/ServiceRequest/';
    $ch = curl_init($url);
    
    $sf_auth = 'Bearer XXXXXXXXX';
    
    $params = ("{\"Fields\":[{\"Name\":\"requestDefinitionId\",\"Value\":\"a3Hf0000000lTNaEAM\"},{\"Name\":\"client\",\"Value\":\"0053j00000A7rWLAAZ\"}],\"Answers\":[{\"QuestionId\":\"a3Df0000000qI63EAE\",\"Values\":[\"Reclama\u00e7\u00e3o\"]},{\"QuestionId\":\"a3Df0000000qHvsEAE\",\"Values\":[\"Solicita\u00e7\u00e3o de Servi\u00e7o aberta por qualquer integra\u00e7\u00e3o web\"]},{\"QuestionId\":\"a3Df0000000qHwREAU\",\"Values\":[\"Web\"]}]}");
               
        $data_string = json_encode($params);
        
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Accept: application/json',
            'Content-type: application/json',
            'Authorization:' . $sf_auth,
        ));
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        //execute post
        $result = curl_exec($ch);
        //close connection
        curl_close($ch);
        
        $response = json_decode(curl_exec($ch), true);
    
        echo $response;
        
        echo curl_errno($ch) . '<br/>';
        echo curl_error($ch) . '<br/>';
    
        var_dump($result);
        
    ?>