执行cURL请求时出错


Error when doing cURL request

此代码总是从API返回用户不存在:

$data2 = array('user'=>$vars['mcusername'],
              'pwd'=>$vars['mcpassword'],
              'group'=>$postfields['group'],
              'action'=>'Save');    
// Connect to dvb API
$configWebAddress = "http://192.168.0.12:4040/dvbapi.html?part=userconfig&";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $configWebAddress);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data2);
$data = curl_exec($ch);
curl_close($ch);

在浏览器中工作的字符串是:

dvbapi.html?part=userconfig&user=PeterTest&pwd=obfuscated&group=1,2&disabled=0&action=Save

当您在浏览器中访问URL时,您正在执行GET。在您的cURL尝试中,您正在尝试POST。这很可能就是问题所在;则脚本可以仅接受CCD_ 3。

请尝试使用此cURL代码:

// Gather up all the values to send to the script
$data2 = array('part'   => 'userconfig',
               'user'   => $vars['mcusername'],
               'pwd'    => $vars['mcpassword'],
               'group'  => $postfields['group'],
               'action' => 'Save');  
// Generate the request URL
$configWebAddress = "http://192.168.0.12:4040/dvbapi.html?".http_build_query($data2);
// cURL the URL for a responce
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $configWebAddress);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
// Show the responce
var_dump($data);

您可以使用http_build_query()将数组转换为URL编码的字符串,以发出GET请求。