我正在读取包含一些地址信息的文件。某些地址无法处理(错误代码 422)。如何返回可处理地址的结果并将错误位置留空?
require 'myFunctions.php';
require_once '../../vendor/autoload.php';
use Stanley\Geocodio\Client;
$client = new Client('Your API key');
//Read in address data.
$AddressData = file('address.txt');
//Send a request to the API.
$location = $client->post($AddressData);
我认为API 会自动处理错误并将它们留空,不是吗?(如果您共享错误的屏幕截图会有所帮助) - 但无论如何,如果您的意思是捕获此类错误(异常),这里是Geocodio中所有可能异常的列表(源):
An HTTP 403 error raises a GeocodioAuthError
An HTTP 422 error raises a GeocodioDataError and the error message will be reported through the exception
An HTTP 5xx error raises a GeocodioServerError
An unmatched non-200 response will simply raise Exception
在您的情况下,对于不可处理的地址,您应该捕获GeocodioDataError
异常。无论如何购买,捕获所有支持的对象是一个好习惯(Catch'Em All)。我想你应该能够做这样的事情:
try
{
$location = $client->post($AddressData);
}
catch(\Stanley\Geocodio\GeocodioAuthError)
{
// do something
}
catch(\Stanley\Geocodio\GeocodioDataError)
{
// do something
}
catch(\Stanley\Geocodio\GeocodioServerError)
{
// do something
}
catch (\Exception $e)
{
// do something
}
在附加的屏幕截图中,我看到您正在使用Guzzle
! 在Guzzle
你可以捕捉这样的异常(更多阅读):
try
{
$location = $client->post($AddressData);
}
catch (Guzzle\Http\Exception\BadResponseException $e)
{
// do something
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句