doujiejujixi27244
2015-04-09 14:59
浏览 69
已采纳

执行cURL请求时出错

This code always returns user doesn't exist from the 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);

The string that works in a browser is this:

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

图片转代码服务由CSDN问答提供 功能建议

此代码始终返回API中不存在的用户:

   $ data2 = array('user'=> $ vars ['mcusername'],
'pwd'=> $ vars ['mcpassword'],
'group'=> $ postfields ['  group'],
'action'=>'保存');  
 
 //连接到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); 
   
 
 

在浏览器中工作的字符串是:< /p>

dvbapi.html?part=userconfig&user=PeterTest&pwd=obfuscated&group=1,2&disabled=0&action=Save
 
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyanqu9722 2015-04-09 16:02
    已采纳

    When you access the URL in the browser, you're performing a GET. In your cURL attempt, you're attempting to POST. This is likely the issue; the script may only accept GET.

    Try using this cURL code instead:

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

    You can use http_build_query() to turn your array into a URL-encoded string to make the GET request.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题