我正试图将这个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 Docs来处理客户端,您将看到您可以为客户端发出的所有请求设置base_url、headers和身份验证。
我下面的例子和Sammitch上面的例子的区别在于,我添加了头和身份验证作为客户端的默认值。这将允许对您的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
]);