I have the following code:
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $this->credentials);
if ($action == 'post') {
curl_setopt($ch, CURLOPT_HTTPHEADER, array ("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_POST, 1);
if(isset($params)){
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
}
}
I am basically trying to mimic the following:
curl --user $APPLICATION_ID:$MASTER_KEY \
-X POST \
-H "Content-Type: application/json" \
-d '{"score": 1337, "playerName": "Sean Plott", "cheatMode": false }' \
https://api.somewebsite.com/1/classes/GameScore
As of now $params is an array, not sure if this is correct or not.. should I json_encode the $params? How do I get rid of the 411 error?
http_build_queryis only for sendingapplication/x-www-form-urlencodeddata, which yours isn’t. Your POST data is probably messed up so you’re not sending aContent-Lengthheader with your request. Assuming you have your params as an array of those key-value pairs, you can use the following: