【问题标题】:FreeAgent oAuth2 can't update contact using Light PHP wrapperFreeAgent oAuth2 无法使用 Light PHP 包装器更新联系人
【发布时间】:2019-11-03 04:45:09
【问题描述】:

我在 FreeAgent 中创建新联系人时遇到问题。

我正在为 OAuth 2.0 协议使用 Light PHP 包装器,并成功设置了 oAuth2。我可以连接到 FreeAgent 并成功检索客户列表。到目前为止,一切都很好。我遇到的问题是以另一种方式发回数据,即在 FreeAgent 中创建新联系人。

这是我尝试过的一些代码:

require_once($_SERVER['DOCUMENT_ROOT'].'/OAuth2/Client.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/OAuth2/GrantType/IGrantType.php');

$client = new OAuth2\Client($clientid, $secret);

$params = json_encode(array('contact' => array('first-name' => $firstname_unencrypted, 'last-name' => $surname_unencrypted)));

$response = $client->fetch("/contacts", $params, "POST", array('User-Agent' => 'MyApp', 'Accept' => 'application/json', 'Content-type' => 'application/json'));

var_dump($response);

返回的 var_dump 显示:

array(3) {
  ["result"]=>
  bool(false)
  ["code"]=>
  int(0)
  ["content_type"]=>
  bool(false)
}

我很确定我在做一些愚蠢的事情。我尝试过发送 XML 而不是 JSON。我试过只使用一个参数数组,而不是我在示例中得到的数组中的数组。我在兜圈子!

如果有人能在正确的方向上给我一点帮助,我将永远感激不尽!

【问题讨论】:

  • 我正在使用 CURL,现在在这方面取得了一些进展,但仍然出现错误。我会进一步调查,如果找到解决方案,我会在这里发布...
  • 是的,CURL 运行良好。早该想到的。站起来!!

标签: php oauth-2.0


【解决方案1】:

有效的示例代码:

            $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
                     <contact>
                      <first-name>$firstname_unencrypted</first-name>
                      <last-name>$surname_unencrypted</last-name>
                    </contact>";

            if ($sandbox || $demo)
                $ch = curl_init('https://api.sandbox.freeagent.com/v2/contacts');
            else
                $ch = curl_init('https://api.freeagent.com/v2/contacts');
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Authorization: Bearer '.$freeagentaccesstoken,
                'Content-Type: application/xml',
                'Accept: application/json',
                'User-Agent: MyApp',
                'Content-Length: ' . strlen($xml))
            );
            curl_setopt($ch, CURLOPT_TIMEOUT, 5);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

            //execute post
            $result=json_decode(curl_exec($ch), true);
            $freeagenturl=$result['contact']['url'];
            curl_close($ch);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多