PHP curl不起作用

时间:2014-09-05 11:05:05

标签: php curl

嘿伙计们试图用卷曲打印一个api。但到目前为止,我无法让它工作。也许你们可以帮助我。该网站使用api用户和传递两者都可以在我到目前为止的代码中查看。它的作用是获取$ original_url并根据template_id(20016)给我们预览。你可以在这里阅读文档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;
希望你们能帮助我

1 个答案:

答案 0 :(得分:2)

似乎你想在不支持POST的网址上进行http POST。

如果您想获得20016模板数据,则必须这样做:

$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);

此处的完整API文档=> Duda mobile api Doc