执行cURL请求时出错

时间:2015-04-09 14:59:07

标签: php curl

此代码始终返回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

1 个答案:

答案 0 :(得分:0)

当您在浏览器中访问该网址时,您正在执行GET。在您的cURL尝试中,您尝试POST。这可能是个问题;该脚本只能接受GET

请尝试使用此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()将数组转换为网址编码字符串,以发出GET请求。