glabsdev / php

Globe Labs PHP sample codes
10 stars 12 forks source link

I got JSON parse error when sending SMS #3

Closed VinceSanityyy closed 4 years ago

VinceSanityyy commented 4 years ago

I used curl in accessing the api. I based it in your docs which is something like this

curl -X POST
"https://devapi.globelabs.com.ph/smsmessaging/v1/outbound/1234/requests?access_token=3YM8xurK_IPdhvX4OUWXQljcHTIPgQDdTESLXDIes4g" -H "Content-Type: application/json" -d
{"outboundSMSMessageRequest": {
   "clientCorrelator": "123456",
   "senderAddress": "1234",
   "outboundSMSTextMessage": {"message": "Hello World"},
   "address": "9171234567"
 }
}

I followed it in php curl and only using REQUIRED parameters by

$ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://devapi.globelabs.com.ph/smsmessaging/v1/outbound/$senderAddress/requests?access_token=$access_token");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_POST,1 );
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
        ));
        $postData = [
            'address' => '+639270277397',
            'message' => 'Hello from API'
        ];
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

        $result=curl_exec ($ch);
        dd($result);

What am i getting is "{"detail":"JSON parse error - Expecting value: line 1 column 1 (char 0)"}"

Can someone tell me what is wrong? Pls correct me.

joyfementira commented 4 years ago

It seem's like you're passing an array instead JSON format.

Change: curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); to curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( array( 'address' => '+639270277397', 'message' => 'Hello from API' ) ) );

VinceSanityyy commented 4 years ago

Yah, thanks for the info @joyfementira . I already resolved it by

  $postData = [
            'address' => '+639270277397',
            'message' => 'Hello from API',
        ];
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));

Seems i did not read well the docs