2

Trying to do this in php

curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" -u xxx:xxx -d '{ "broadcast": true, "title": "Hello World", "message": "from Kumulos Push" }' "https://push.kumulos.com/notifications" 

In php I have this...

$ch = curl_init( 'https://push.kumulos.com/notifications' ); curl_setopt_array( $ch, [ CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_HEADER => [ 'Content-Type: application/json', 'Accept: application/json', 'Content-Length: ' . strlen( $data ), ], CURLOPT_USERPWD => 'xxx:xxx', CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => $data, ] ); $resp = htmlentities( curl_exec( $ch ) ); 

Still in php I get redirected whereas in command line I get the expected response...

UPDATE

It returns this HTML in PHP, whereas in command line I get expected JSON

<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <meta http-equiv="refresh" content="1;url=https://push.kumulos.com" /> <title>Redirecting to https://push.kumulos.com</title> </head> <body> Redirecting to <a href="https://push.kumulos.com">https://push.kumulos.com</a>. </body> </html> 

Update : data sent with php request

$data = json_encode( [ "title" => "Hello World", "message" => $message, "installIds" => [ $deviceToken, ], ] ); 

Update : Expected output ( received in command line curl )

{ "appId":9999, "source":2, "status":1, "filters":{ "installIds":[ "xxx" ] }, "title":"Test", "message":"3 new questions on Tomorrow Times!", "data":null, "isBackgroundData":false, "url":null, "targetedRecipients":0, "expectedResolutionSteps":0, "completedResolutionSteps":0, "expectedBatches":0, "completedBatches":0, "updatedAt":"2017-06-16T04:58:54+0000", "createdAt":"2017-06-16T04:58:54+0000", "id":13976 } 

2 Answers 2

2

You need to set curlopt_followlocation => 1. Also what are you expecting to return? If you are -xpecting something other than a truthy falsy value you need to remove curlopt_returntransfer. Also if you are sending post just use CURLOPT_POST => 1


Example

$ch = curl_init( 'https://push.kumulos.com/notifications' ); curl_setopt_array( $ch, [ CURLOPT_POST => 1, CURLOPT_HEADER => [ 'Content-Type: application/json', 'Accept: application/json', 'Content-Length: ' . strlen( $data ), ], CURLOPT_USERPWD => 'xxx:xxx', CURLOPT_FOLLOWLOCATION => 1, CURLOPT_POSTFIELDS => $data, ] ); $resp = htmlentities( curl_exec( $ch ) ); 

Since you are using

htmlentities(...) 

I definitely assume you need to remove CURLOPT_RETURNTRANSFER.

CURLOPT_RETURNTRANSFER

TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.


CURLOPT_FOLLOWLOCATION

TRUE to follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set).


Edit

To address new information from the user. Please the Kumulos documentation. Are you sure this is a post request and not a put request? Do you need to send an API key? Can you post the JSON data you are sending?

From our conversation it appears you now are recieving a 200 instead of a 3XX after the changes were implemented. Please update question accordingly. The issue now is somewhere in your data I believe

Sign up to request clarification or add additional context in comments.

12 Comments

Thanks :) It returns JSON containing details about the action performed... ATM it returns some HTML...
Updated question with response php curl request returns... Setting CURLOPT_FOLLOWLOCATION to false didn't do the trick for me...
Are you sure this is a post request and not a put request? Do you need to send an API key? Can you post the JSON data you are sending?
Updated question :) Can you share the link to docs you are referring to?
and yes, using POST ;)
|
1

Mark from Kumulos Tech Support here.

The problem is caused by the absence of HTTP headers in the request.

CURLOPT_HEADER is used (true or false) to indicate whether or not headers will be included in the request.

To actually set the headers you need to use CURLOPT_HTTPHEADER

Without the headers, the request will be redirected as you have seen.

Please find below some example PHP code to send a broadcast push to all subscribed installs of your app using the Kumulos Push API.

#!/usr/bin/php <?php $postData = json_encode( array ( "broadcast" => true, "title" => "Test Broadcast", "message" => "Test from PHP" ) ); $curl = curl_init(); curl_setopt_array( $curl, [ CURLOPT_URL => "https://push.kumulos.com/notifications", CURLOPT_HTTPHEADER => array ( 'content-type: application/json', 'accept: application/json', 'content-length: ' . strlen($postData), ), CURLOPT_USERPWD => 'apiKey:serverKey', CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => $postData, CURLOPT_RETURNTRANSFER => true ] ); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { echo $response; } ?> 

1 Comment

Thanks a ton Mark!!! I knew I must be missing on something fundamental like this but couldn't figure out what... 😅 Thanks again! Have a lovely day! 😊

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.