调用darabonba-openapi提供的接口发起请求。
darabonba-openapi的github地址为:
https://github.com/alibabacloud-sdk-php/darabonba-openapi
<?php use AlibabaCloud\Tea\Tea; use AlibabaCloud\Tea\Utils\Utils; use AlibabaCloud\OpenApiUtil\OpenApiUtilClient; use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions; use AlibabaCloud\SDK\Addresspurification\V20191118\Models\ExtractAddressResponse; use Darabonba\OpenApi\Models\Config; use Darabonba\OpenApi\Models\OpenApiRequest; use Darabonba\OpenApi\Models\Params; use Darabonba\OpenApi\OpenApiClient; class Sample { public static function createClient($accessKeyId, $accessKeySecret){ $config = new Config([ "accessKeyId" => $accessKeyId, "accessKeySecret" => $accessKeySecret ]); $config->endpoint = "address-purification.cn-hangzhou.aliyuncs.com"; return new OpenApiClient($config); } public static function main($args){ $client = self::createClient("accessKeyId", "accessKeySecret"); $params = new Params([ 'action' => 'ExtractAddress', 'version' => '2019-11-18', 'protocol' => 'HTTPS', 'pathname' => '/', 'method' => 'POST', 'authType' => 'AK', 'style' => 'RPC', 'reqBodyType' => 'formData', 'bodyType' => 'json', ]); $query = []; $query['AppKey'] = 'Axqwerasdf1a'; $query['ServiceCode'] = 'addrp'; $query['Text'] = '文一西路969号'; $req = new OpenApiRequest([ 'query' => OpenApiUtilClient::query($query), ]); $runtime = new RuntimeOptions([ 'maxIdleConns'=>3, 'connectTimeout'=>10000, 'readTimeout'=>10000 ]); $resp = ExtractAddressResponse::fromMap($client->callApi($params, $req, $runtime)); echo Utils::toJSONString(Tea::merge($resp)); } } $path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php'; if (file_exists($path)) { require_once $path; } Sample::main(array_slice($argv, 1));
IDE:Eclipse PHP
Dependencies:alibabacloud/address-purification-20191118
注意:一开始调试时报错:Request parameters has malformed encoded characters.
经过排查应该是Text参数传入中文导致,在Eclipse PHP中修改编码方式
Windows -- Preferences -- General -- Workspace
在 Text file encoding 中选择 Other,并输入我们想要的编码(这里是 UTF-8),按确定即可。
参考文档:
https://www.cnblogs.com/phpbin/archive/2012/09/26/2704397.html