Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vintrix's avatar

JSON cURL POST retrieving as Array

I am sending a JSON encoded array via POST using cURL on Laravel v7.30.4.

Example of the content being sent:

		    '[
		  "account" => "AD0072001"
		  "notificationType" => "MessageReceived"
		  "id" => "6b9727be-e9c5-4a9f-9331-8de104de8188"
		  "recipient" => 12345
		  "sender" => "xxxxxxxxxxxx"
		  "content" => "Test A B"
		  "sentAt" => "2024-02-17 12:26:29Z"
		]', JSON_PRETTY_PRINT); ```

Set up the cURL
``` $curlHandle = curl_init($this->URL);
		curl_setopt( $curlHandle, CURLOPT_FAILONERROR, 1 );
		curl_setopt( $curlHandle, CURLOPT_FOLLOWLOCATION, 1 );	// Allow redirects.
		curl_setopt( $curlHandle, CURLOPT_RETURNTRANSFER, 1 ); 	// Return into a variable.
		curl_setopt( $curlHandle, CURLOPT_TIMEOUT, 30 ); 		// Times out after 30 seconds.
curl_setopt( $curlHandle, CURLOPT_HEADER, 
                ['accept: application/json',
    			'content-type: application/json']
              );
curl_setopt($curlHandle, CURLOPT_ENCODING, 'gzip');
		curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, 'POST');
		curl_setopt($curlHandle, CURLOPT_PORT, $this->port );				// Set the port number.
		curl_setopt($curlHandle, CURLOPT_POST, 1);
		curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $this->BodyContent);
		curl_setopt($curlHandle, CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
		curl_setopt($curlHandle, CURLOPT_USERPWD, $this->AuthUser . ":" . $this->AuthPass);
$results = curl_exec($curlHandle); ```

However, the recipient is receiving the POST as an array with the content as the Key of the array.
```'{"account":"AD0072001","notificationType":"MessageReceived","id":"6b9727be-e9c5-4a9f-9331-8de104de8188","recipient":12345,"sender":"xxxxxxxxxxxx","content":"Test_A_B","sentAt":"2024-02-17_14:07:22Z"}' => ''```

How do I send the JSON so that it is received as JSON and not an array?
0 likes
1 reply
kleninmaxim's avatar

Your question text is poorly styled to understand what is written. Please send a clearer code and question.

Please or to participate in this conversation.