开发者社区 问答 正文

钉钉获取持久码为空

扫码登录获取用户授权的持久授权码时获取为空,打印的结果如下:
url:oapi域名/sns/get_persistent_code?access_token=608f12dcecc335daa1e948731b31479b
post_data:array(1) {
  ["tmp_auth_code"]=>
  string(32) "2f7b09ac3e43310ca735f1da7a75ff89"
}
json_encode post_data:{"tmp_auth_code":"2f7b09ac3e43310ca735f1da7a75ff89"}
return:array(2) {
  [0]=>
  int(0)
  [1]=>
  string(0) ""
}


使用的php源码为:
$url="oapi域名/sns/get_persistent_code?access_token=".$access_token;
$post_data = array('tmp_auth_code' => $tmp_auth_code);
$object=http_post_data($url,json_encode($post_data));
echo "<br>return:";
var_dump($object);

function http_post_data($url, $data_string) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
   'Content-Type: application/json; charset=utf-8',
   'Content-Length: ' . strlen($data_string))
  );
        ob_start();
        curl_exec($ch);
        $return_content = ob_get_contents();
        ob_end_clean();
        $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        return array($return_code, $return_content);
    }




请问这是什么原因?post的数据看起来没问题啊,怎么没有获取到值,如果不是POST倒是会提示错误

展开
收起
易联老王 2017-05-25 23:39:49 2356 分享 版权
1 条回答
写回答
取消 提交回答
  • Re钉钉获取持久码为空
    修改函数http_post_data如下还是不行
    function http_post_data($url, $jsonStr)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($jsonStr)
            )
        );
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        return array($httpCode, $response);
    }
    2017-05-26 09:49:03
    赞同 展开评论