我试着把这个卷曲呼叫转换成口香糖:
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);谢谢!
发布于 2015-03-17 17:50:59
它应该是这样的:
$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凭据。
发布于 2015-03-17 21:28:50
如果您引用用于使用客户端的口吻文档,您将看到可以为所有与客户端发出的请求设置base_url、标头和身份验证。
下面的示例与上面的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
]);https://stackoverflow.com/questions/29105512
复制相似问题