企业微信 PHP 通过接口获取客户的 用户id demo
1企业微信 PHP 通过接口获取客户的 用户id demo
由于企业微信客户端目前只能在企业微信中使用,所以需要先将客户加入到企业微信的客户联系人中,然后通过客户联系人相关接口获取客户的用户id。

以下是通过 API 获取客户的用户id的示例代码(需使用企业微信应用的 Secret 和 CorpID):

```

$corpId = "企业微信的CorpID";
$secret = "企业微信应用的Secret";
$customerId = "客户联系人的userid";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=".$corpId."&corpsecret=".$secret);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$res = curl_exec($ch);
curl_close($ch);

$res = json_decode($res, true);
$accessToken = $res["access_token"];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get?access_token=".$accessToken."&external_userid=".$customerId);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$res = curl_exec($ch);
curl_close($ch);

$res = json_decode($res, true);
$userId = $res["external_contact"]["follow_user"];

echo "客户的userid为:".$userId;

?>
```

代码中,首先通过企业微信的获取 access_token 的接口获取 access_token,然后通过客户联系人相关接口获取客户的用户id(即 follow_user 值)。

注意:此示例代码中的 $customerId 是客户联系人的 userid,而不是微信号或手机号等其他形式的标识。客户联系人的 userid 可以通过客户联系人相关接口获取。
本页由《梦行文档》生成

 

name完成
30