全部产品
云市场

PHP使用示例

更新时间:2019-05-07 16:46:12

PHP使用示例

需要阿里云PHP SDK

  1. ?php
  2. require_once '/path/to/aliyun-openapi-php-sdk/aliyun-php-sdk-core/Config.php';
  3. class NlpRequest extends RoaAcsRequest{
  4. function __construct(){
  5. parent::__construct('Nlp', '2018-04-08', 'wordsegment');
  6. $this->setUriPattern("/nlp/api/wordsegment/general");
  7. $this->setMethod("POST");
  8. }
  9. }
  10. # 创建DefaultAcsClient实例并初始化
  11. $clientProfile = DefaultProfile::getProfile(
  12. "cn-shanghai", #目前支持持cn-shanghai
  13. "youraccesskeyid", # 您的 AccessKey ID
  14. "youraccesskeysecret" # 您的 AccessKey Secret
  15. );
  16. $clientProfile->addEndpoint('cn-shanghai', "cn-shanghai", "Nlp", "nlp.cn-shanghai.aliyuncs.com");
  17. $client = new DefaultAcsClient($clientProfile);
  18. $request = new NlpRequest();
  19. $request->setContent('{"lang":"ZH","text":"Iphone专用数据线"}');
  20. try {
  21. $response = $client->getAcsResponse($request);
  22. print_r($response);
  23. } catch(ServerException $e) {
  24. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  25. } catch(ClientException $e) {
  26. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  27. }
  28. ?>