在从xe转换器api获取数据时遇到问题

时间:2018-06-12 07:49:26

标签: php

您好我正在使用XE转换器api并需要从网址获取数据,到目前为止我的代码是

$new_url = "https://xecdapi.xe.com/v1/account_info/";
$getData = CurlSendPostRequest($new_url);

print_r($getData);


function CurlSendPostRequest($url)
{
        $ch = curl_init($url);
        $options = array(
                CURLOPT_RETURNTRANSFER => true,         // return web page
                CURLOPT_HEADER         => false,        // don't return headers
                CURLOPT_FOLLOWLOCATION => false,         // follow redirects
               // CURLOPT_ENCODING       => "utf-8",           // handle all encodings
                CURLOPT_AUTOREFERER    => true,         // set referer on redirect
                CURLOPT_CONNECTTIMEOUT => 20,          // timeout on connect
                CURLOPT_TIMEOUT        => 20,          // timeout on response
                CURLOPT_POST            => 1,            // i am sending post data
                CURLOPT_POSTFIELDS     => null,    // this are my post vars
                CURLOPT_SSL_VERIFYHOST => 0,            // don't verify ssl
                CURLOPT_SSL_VERIFYPEER => false,        //
                CURLOPT_VERBOSE        => 1,
                CURLOPT_HTTPHEADER     => array(
                    'account_id:mr.125620728',
                    'api_key:d36edshc876f0p5bjge2jujr45'
                )

        );

        curl_setopt_array($ch,$options);
        $data = curl_exec($ch);
        $curl_errno = curl_errno($ch);

        curl_close($ch);
        return $data;
    }

但我收到错误未找到。我尝试了很多功能,但却有所作为。看起来在传递标题时遇到问题

在文档中,它写的是这样的

curl –i -u account_id:api_key "https://xecdapi.xe.com/v1/account_info/"

但没有获取信息

由于

此致

2 个答案:

答案 0 :(得分:0)

交换机-u account_id:api_key用于指定用户名:密码。 来自curl --help

 -u, --user USER[:PASSWORD]  Server user and password

因此,您需要以mr.125620728 / d36edshc876f0p5bjge2jujr45

进行身份验证

您尚未指定XE API支持的身份验证方案,但假设它是基本身份验证,那么您可以使用

CURLOPT_USERPWD => $username . ":" . $password

在您的案例中$username=mr.125620728$password=d36edshc876f0p5bjge2jujr45

另见How do I make a request using HTTP basic authentication with PHP curl?

答案 1 :(得分:0)

我知道我来晚了,但是下面是一个有效的示例,只需更改您的帐户ID和api键

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://xecdapi.xe.com/v1/account_info");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

curl_setopt($ch, CURLOPT_USERPWD, 'apifixer.com504567390' . ':' . 'miso4ofghfghfh1jbp733jndg');

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);
echo $result;