Filipe Costa
Filipe Costa

Reputation: 15

PHP curl doesn't work

Hey guys im trying to print one api using curl. but so far i wasnt able to get it to work. maybe you guys can help me. the website uses api user and pass both can be viewed in the code i made so far. what this does is it gets the $original_url and gives us a preview based on the template_id which is 20016. you can read the documentation here https://support.dudamobile.com/API/API-Use-Cases/Multiscreen-White-Label-Setup

$original_url = "http://rsportugal.org/index.html";
$data = array("template_id"=>"20016","url"=>$original_url);    
$data = json_encode($data);

define("API_USER","...");
define("API_PASS","...");

$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.dudamobile.com/api/sites/multiscreen/templates');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");    

$output = curl_exec($ch);

if(curl_errno($ch)) {
    die('Curl error: ' . curl_error($ch));
}   
$output = json_decode($output);

curl_close($ch);

return $output->site_name;

hopefully you guys can help me

Upvotes: 0

Views: 680

Answers (1)

Paul Giragossian
Paul Giragossian

Reputation: 375

It seems you want to do a http POST on an url that doesn't support POST.

If you want to get 20016 template data, you have to do so :

$templateId = 20016;

define("API_USER","..."); 
define("API_PASS","...");

$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.dudamobile.com/api/sites/multiscreen/templates/' . $templateId);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");    

$output = curl_exec($ch);

if(curl_errno($ch)) {
    die('Curl error: ' . curl_error($ch));
}   
$output = json_decode($output);

print_r($output);

curl_close($ch);

Full API documentation here => Duda mobile api Doc

Upvotes: 2

Related Questions