是否有任何方法将convery curl命令转换为php curl

时间:2015-04-29 18:29:34

标签: php json api curl

我是服务器管理员,我对PHP没有任何了解。

任何人都可以帮我把这个curl命令转换成php curl

curl -H "Content-Type: application/json" -X POST -d '{"SystemsTraceAuditNumber":"455690","RetrievalReferenceNumber":"405060908072","AcquiringBin":"409999","AcquirerCountryCode":"101","PrimaryAccountNumber":"4895070000008881"}'
-v --ciphers RC4-SHA:RC4-MD5 https://sandbox.visa.com/rsrv_vpp/v1/acnl -u 72cdcdd2-9ccd-4adc-8827-0324324523423414a:f42342317-b62340-42343e-8234-e2342342341357b0
--cacert ./SMCAroot.crt --cert /home/sourcecode/sandbox_cert.pem -k

我试过转换代码但搞砸了所有内容,我正在使用php获得http 500响应。

但是当我在linux shell上执行curl命令时,它的工作正常

以下是我在php中转换的代码

<?php
$data = array(
   'SystemsTraceAuditNumber' => '455690',
   'RetrievalReferenceNumber' => '405060908072',
   'AcquiringBin' => '409999',
   'AcquirerCountryCode' => '101',
   'PrimaryAccountNumber' => '4895070000008881',
);
//echo $data;
$data_string = json_encode($data);

$handle=curl_init('https://sandbox.visa.com/rsrv_vpp/v1/acnl');

curl_setopt($handle, CURLOPT_VERBOSE, true);
curl_setopt($handle, CURLOPT_SSL_CIPHER_LIST, 'RC4-SHA:RC4-MD5');
curl_setopt($handle, CURLOPT_USERPWD, 'xxxxxxxxx:xxxxxxxxxxxxxxxxxxx');
curl_setopt($handle, CURLOPT_SSLCERT, 'sandbox_cert.pem');
curl_setopt($handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 

curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST");

curl_setopt($handle, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json')
);
$handle;

$content = curl_exec($handle);

if(!curl_errno($handle))
{
    $info = curl_getinfo($handle);
    echo "<pre>";
    print_r($info);
    echo "</pre>";

    //echo '<br/> Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
}
else
{
    'Curl error: ' . curl_error($handle);
}
$content;

?>

1 个答案:

答案 0 :(得分:0)

可能更容易设置它们:

$defaults = array(
        CURLOPT_VERBOSE => 1,
        CURLOPT_SSL_CIPHER_LIST => 'RC4-SHA:RC4-MD5',
        CURLOPT_USERPWD => 'xxxxxxxxx:xxxxxxxxxxxxxxxxxxx',
        CURLOPT_SSLCERT => '/home/sourcecode/sandbox_cert.pem',
        CURLOPT_URL => 'https://sandbox.visa.com/rsrv_vpp/v1/acnl',
        CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
        CURLOPT_POST => 1,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_SSL_VERIFYPEER => 0,
        CURLOPT_POSTFIELDS => json_encode($data),
        CURLOPT_HTTPHEADER => array('Content-Type: application/json')
);

$handle = curl_init();
curl_setopt_array($handle, ($options + $defaults));
if( ! $content = curl_exec($handle)){
    trigger_error(curl_error($handle));
}
curl_close($ch);
echo $content;