We are attempting to post to a rest API endpoint that requires a "POST object" within one of the keys. In javascript/jquery, this works fine. But, using CURL in PHP the endpoint doesn't receive the object (called "components") here:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, 'http://api.sitesitesite.com');
$comps = array('slug' => "xyz",
'visble' => 1,
'color' => "xyz",
'shape' => "xyz",
'version' => "2",
);
$post_args = array();
$post_args['components'] = $comps;
$post_args['id'] = $id;
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_args);
$result = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Get rid of "CURLOPT_CUSTOMREQUEST" and use "CURLOPT_POST"
curl_setopt($ch, CURLOPT_POST, true);
Related
I am using a CURL post to an existing API. The API returns the error content must be JSON or plain text. I json_encode data and post the encoded data to the API.
In the API, it says the Content-Type should be application/x-www-form-urlencoded'.
However, it still returns the error content must be JSON or plain text. What could be the issue with my code below after submitting my data in JSON format?
This is a sample request body in the API doc.
"item" : "Store",
"content" : {
"channel":"false",
"hop": "false",
"msg": "Order successfully placed."
}
Controller
public function postUrl()
{
$url = "https://api.com/";
$data = array("item" => "Nike Shoes","content" => array ("channel" => "false" ,"hop" => "false","msg" => "Item Sold"));
$postdata = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$result = curl_exec($ch);
curl_close($ch);
Log::info($postdata);
}
Perhaps this is because boolean values are specified as strings.
public function postUrl()
{
$url = 'https://api.com/';
$data = [
'item' => 'Nike Shoes',
'content' => [
'channel' => false,
'hop' => false,
'msg' => 'Item Sold'
]
];
$postData = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$result = curl_exec($ch);
curl_close($ch);
Log::info($postData);
}
Also try this
public function postUrl()
{
$url = 'https://api.com/';
$data = [
'item' => 'Nike Shoes',
'content' => [
'channel' => 'false',
'hop' => 'false',
'msg' => 'Item Sold'
]
];
$postData = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: text/json']);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$result = curl_exec($ch);
curl_close($ch);
Log::info($postData);
}
I am working with the Challonge.com API found here: https://api.challonge.com/v1
I am trying to get the match UPDATE feature to work - https://api.challonge.com/v1/documents/matches/update
I have had success updating my tournament with the same code, but for some reason the following code is not updating the variables. Instead the response I get is the same as before the script is ran. Any Ideas?
// Update Match on Challonge
$params = array(
"api_key" => "my api key goes here",
"winner_id" => "50287554",
"scores_csv" => "2-5,1-3"
);
$url = "https://api.challonge.com/v1/tournaments/efps_59/matches/78842711.json";
$data_json = json_encode($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_json)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
Your documentation states that the parameters for winner_id and scores_csvhave to be an array of match:
// Update Match on Challonge
$params = array(
"api_key" => "my api key goes here",
"match" => array(
"winner_id" => "50287554",
"scores_csv" => "2-5,1-3"
)
);
I'm using GCM in php. Everything is fine. But im getting response as
"Field \"data\" must be a JSON array: example\n"
My code for GCM is
function sendNotification($registrationIdsArray, $messageData) {
$data = array(
'data' => $messageData,
'registration_ids' => $registrationIdsArray
);
var_dump($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
and my $data object is
{"data":"example","registration_ids":["apikey1", "apikey2"]}
What is the missing code for GCM here.
the data which you are passing should be an array not a string like this
$messageData = array(
'success' => 1,
'title' => $data['title'],
'desc' => $data['message']
);
I have a few subscriptions. Time to time subscription ends and I start the ended subscription again.
Now, my most used subscriptions ended and I can't start again. I'm working in PHP. Nothing has changed in my code, but now I get error while trying to subscribe.
{"meta":{"error_type":"APISubscriptionError","code":400,"error_message":"Invalid response"}}
My other subscriptions are still active but 4 of them are failing. I couldn't fix it.
Edit - Code:
<?php
echo $_GET['hub_challenge'];
$client_id='XXXXX';
$client_secret='XXXXX';
$object = 'tag';
$aspect = 'media';
$object_id = 'XXXXX';
$verify_token='XXXXX';
$callback_url = 'http://XXXXX/callback.php';
$attachment = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'object' => $object,
'object_id' => $object_id,
'aspect' => $aspect,
'verify_token' => $verify_token,
'callback_url'=>$callback_url
);
$url = "https://api.instagram.com/v1/subscriptions/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close ($ch);
print_r($result);
Instagram sends a GET request to uour callback_url and wants you to response with hub.challenge.
I have the following code in php:
define("TOKEN_URL", "https://wamsprodglobal001acs.accesscontrol.windows.net/v2/OAuth2-13");
$arrData = array(
'grant_type=client_credentials',
'client_id='.CLIENT_ID,
'client_secret='.urlencode(ACCESS_KEY),
'scope=urn%3aWindowsAzureMediaServices'
);
$arrHeader = array(
'Content-length:'.strlen($this->generateData($arrData))
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, TOKEN_URL);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->generateData($arrData));
curl_setopt($ch, CURLOPT_HTTPHEADER, $arrHeader);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$arrToken = json_decode($data);
I am unable to get the token code. Please can anyone check what could be wrong?
There could be a few issues:
You could simplify a few things and use http_build_query():
$data = http_build_query(array(
'grant_type' => 'client_credentials',
'client_id' => CLIENT_ID,
'client_secret' => ACCESS_KEY,
'scope' => 'urn:WindowsAzureMediaServices',
));
$ch = curl_init(TOKEN_URL);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (($res = curl_exec($ch)) === false) {
die(curl_error($ch));
}
$arrToken = json_decode($res);
If there's an error, the first thing to make sure is whether you have an updated list of CA certificates.