将 curl 转换为 guzzle for spreedly API
convert curl to guzzle for spreedly API
我正在尝试将此 curl 调用转换为 guzzle:
curl https://core.spreedly.com/v1/gateways/CAL6uXHtGfDsxV2kW6apTP5JhG/purchase.xml \
-u 'Ll6fAtoVY5hTGlJEmtpo5YTS:RKOCG5D8jhgfdDSg524u5iF22XD4Io5VXmyzdCptrvHFTTSy' \
-H 'Content-Type: application/xml' \
-d '<transaction>
<payment_method_token>ZgPNHes541EMlBN86glRDKRexzq</payment_method_token>
<amount>100</amount>
<currency_code>USD</currency_code>
</transaction>'
我知道它经常被使用,所以我们将不胜感激
这是我尝试过的方法,但我总是收到此错误“客户端错误响应
[状态码] 422
[原因短语] 无法处理的实体
$xml = '<transaction>\n';
$xml .= '<payment_method_token>$payment_method_token</payment_method_token>\n';
$xml .= '<amount>100</amount>\n';
$xml .= '<currency_code>USD</currency_code>\n'
$xml .= '<transaction>\n';
$headers = ['Content-Type' => 'application/xml', 'auth' => ['Ll6fAtoVY5hTGlJEmtpo5YTS', 'RKOCG5D8jhgfdDSg524u5iF22XD4Io5VXmyzdCptrvHFTTSy']];
$client = new Client('https://core.spreedly.com/v1/gateways/CAL6uXHtGfDsxV2kW6apTP5JhG/purchase.xml');
$requestCurl = $client->post('', $headers, $xml,[]);
$response = $requestCurl->send()->xml();
dd($response);
谢谢!
它应该看起来像这样:
$client = new Client('https://core.spreedly.com/v1/gateways/merchant_id');
$xml = '<...>';
$options = [
'headers' => [ 'Content-Type' => 'application/xml' ],
'auth' => ['username', 'password'],
'body' => $xml,
];
$response = $client->post('/purchase.xml', $options);
并且您可能想再次查看您的代码并弄清楚您是否不小心公开了您的 API 凭据。
如果您参考 Guzzle 使用 Clients 的文档,您将看到您可以为所有请求设置 base_url、header 和身份验证与客户一起制作。
我下面的示例与上面 Sammitch 的示例之间的区别在于我添加了 header 和身份验证作为客户端的默认值。这将允许对您的 api 进行后续调用,而不必将这些作为选项添加到每个请求中。
出于故障排除目的,我只是附加了 LogSubscriber,以便 http 请求和响应随时可用。
$client = new GuzzleHttp\Client([
'base_url' => ['https://core.spreedly.com/{version}/gateways/{merchant_id}/', [
'version' => 'v1',
'merchant_id' => $merchant_id
]],
'defaults' => [
'headers' => ['Content-Type' => 'application/xml'],
'auth' => [$username, $pw],
]]);
/**
* Attach a log subscriber is configured to log the full request and response message using echo() calls.
**/
$client->getEmitter()->attach(new GuzzleHttp\Subscriber\Log\LogSubscriber(null, GuzzleHttp\Subscriber\Log\Formatter::DEBUG));
$response = $client->post('purchase.xml', [
'body' => $xml
]);
我正在尝试将此 curl 调用转换为 guzzle:
curl https://core.spreedly.com/v1/gateways/CAL6uXHtGfDsxV2kW6apTP5JhG/purchase.xml \
-u 'Ll6fAtoVY5hTGlJEmtpo5YTS:RKOCG5D8jhgfdDSg524u5iF22XD4Io5VXmyzdCptrvHFTTSy' \
-H 'Content-Type: application/xml' \
-d '<transaction>
<payment_method_token>ZgPNHes541EMlBN86glRDKRexzq</payment_method_token>
<amount>100</amount>
<currency_code>USD</currency_code>
</transaction>'
我知道它经常被使用,所以我们将不胜感激
这是我尝试过的方法,但我总是收到此错误“客户端错误响应 [状态码] 422 [原因短语] 无法处理的实体
$xml = '<transaction>\n';
$xml .= '<payment_method_token>$payment_method_token</payment_method_token>\n';
$xml .= '<amount>100</amount>\n';
$xml .= '<currency_code>USD</currency_code>\n'
$xml .= '<transaction>\n';
$headers = ['Content-Type' => 'application/xml', 'auth' => ['Ll6fAtoVY5hTGlJEmtpo5YTS', 'RKOCG5D8jhgfdDSg524u5iF22XD4Io5VXmyzdCptrvHFTTSy']];
$client = new Client('https://core.spreedly.com/v1/gateways/CAL6uXHtGfDsxV2kW6apTP5JhG/purchase.xml');
$requestCurl = $client->post('', $headers, $xml,[]);
$response = $requestCurl->send()->xml();
dd($response);
谢谢!
它应该看起来像这样:
$client = new Client('https://core.spreedly.com/v1/gateways/merchant_id');
$xml = '<...>';
$options = [
'headers' => [ 'Content-Type' => 'application/xml' ],
'auth' => ['username', 'password'],
'body' => $xml,
];
$response = $client->post('/purchase.xml', $options);
并且您可能想再次查看您的代码并弄清楚您是否不小心公开了您的 API 凭据。
如果您参考 Guzzle 使用 Clients 的文档,您将看到您可以为所有请求设置 base_url、header 和身份验证与客户一起制作。
我下面的示例与上面 Sammitch 的示例之间的区别在于我添加了 header 和身份验证作为客户端的默认值。这将允许对您的 api 进行后续调用,而不必将这些作为选项添加到每个请求中。
出于故障排除目的,我只是附加了 LogSubscriber,以便 http 请求和响应随时可用。
$client = new GuzzleHttp\Client([
'base_url' => ['https://core.spreedly.com/{version}/gateways/{merchant_id}/', [
'version' => 'v1',
'merchant_id' => $merchant_id
]],
'defaults' => [
'headers' => ['Content-Type' => 'application/xml'],
'auth' => [$username, $pw],
]]);
/**
* Attach a log subscriber is configured to log the full request and response message using echo() calls.
**/
$client->getEmitter()->attach(new GuzzleHttp\Subscriber\Log\LogSubscriber(null, GuzzleHttp\Subscriber\Log\Formatter::DEBUG));
$response = $client->post('purchase.xml', [
'body' => $xml
]);