我在FreeAgent中创建新联系人时遇到问题。
我正在将Light PHP包装器用于OAuth 2.0协议,并已成功设置了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发送。我尝试仅使用一个参数数组,而不是示例中使用的数组中的数组。我要转圈!
如果有人可以在正确的方向上给我一点微调,我将永远感激不已!
答案 0 :(得分:0)
有效的示例代码:
$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);