I’m having problems creating a new contact in FreeAgent.
I’m using the Light PHP wrapper for the OAuth 2.0 protocol and have managed to successfully set up oAuth2. I can connect to FreeAgent and successfully retrieve a list of customers. So far, so good. The problem I’ve got is sending data back the other way, i.e. creating a new contact in FreeAgent.
Here’s some code I’ve tried:
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);
The var_dump returned shows:
array(3) {
["result"]=>
bool(false)
["code"]=>
int(0)
["content_type"]=>
bool(false)
}
I’m pretty sure I’m doing something silly. I’ve tried sending in XML instead of JSON. I’ve tried using just a single array of params rather than the array in an array I’ve got in the sample. I’m going round in circles!
If someone could give me a little nudge in the right direction, I’d be eternally grateful!
Sample code that worked:
$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);
Related
I would like to create new categories in CS-Cart through its API.
So far I have this code (running it through browser, just for testing):
$cfg = get_config(); //connection to DB
$product_data = array();
$product_data["category"] = "Category Test API";
$product_data["company_id"] = 1;
$product_data["status"] = "A";
//CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_USERPWD, 'USER EMAIL'.":".'YOUR API:KEY');
curl_setopt($ch, CURLOPT_URL, $cfg["cscart_store_url"]."api/categories/");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($product_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
if( !curl_error($ch) ) {
echo "no error";
curl_close($ch);
return json_decode($server_output, TRUE);
}
else {
echo "error found!!!";
print_r("Error: ".curl_error($ch));
return 0;
}
There is a documentation here: https://docs.cs-cart.com/latest/developer_guide/api/entities/categories.html.
But I still couldn't make it work although I did not get any errors from curl_exec.
Your json is well encoded, your header have the Content-Type, i think your problem is your cscart_store_url who have a wrong url or your USERPWD isn't good (I believe you didnt put USER EMAIL in your auth
As long as I understood, cs-cart API has some required values in order to make an INSERT API call. Thus, the categories should be already be created inside cs-cart dashboard and match them somehow in the PHP script
On the other hand, if you want to UPDAtE a product for example, you could UPDAte only the specified values you send to API call, without the need of sending all the required API values.
Last but not least, there are more than one Authorizations methods to make the call. I used CURL in order to do the API call and there is a parameter CURLOPT_HTTPHEADER which I have added the below code:
...
CURLOPT_HTTPHEADER => array(
"Authorization: Basic XXXxxxxXXXXxxxXXXlzLmNvbS5ncjpKejMXXXxxxxXN0Y2MDQxenprbEXXXxxXXXE3Mg==",
"Content-Type: application/json",
"cache-control: no-cache"
),
...
Where Authorization is: Basic base64(user#example.com:API_KEY_taken_from_cscart)
Applying all the above the code and the API worked!
thanks everyone for the help and comments
I have been struggling with the youtube alot.
The problem is, i have made my own php curl script which should connect to the youtube api and send a livechat message to my current stream.
I am so close to finishing this issue but only this 1 last part stops me.
The script i currently use is:
$data = array("snippet" => ["type" => 'textMessageEvent', 'textMessageDetails' => ['messageText' => '<3']], 'livechatid' => '{{livechatid_here}}');
$data_string = json_encode($data);
$ch = curl_init('https://www.googleapis.com/youtube/v3/liveChat/messages?part=snippet&fields=authorDetails%2Ckind%2Csnippet&key={{Here is my key}}');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'Authorization: Bearer {{access_key}} ')
);
$result = curl_exec($ch);
dd($result);
But im constantly getting the following error:
"code": 400,
"message": "snippet.live_chat_id is required"\n
So what i did was i changed 'livechatid' to:
liveChatId (as API tells me)
live_Chat_Id
livechatid
snippet.livechatid
snippet.liveChatId
snippet.live_chat_id
And none of them worked.
Does anybody know how i can fix this?
Ofcourse i have been deleting my keys and access_tokens from the code above.
I have also tried adding them to the header but i still get the same error again and again.
Does anybody know how i can solve this problem?
Put liveChatId inside the snippet field :
$data = array("snippet" => [
"type" => 'textMessageEvent',
'textMessageDetails' => ['messageText' => '<3'],
'liveChatId' => 'YOUR_LIVE_CHAT_ID'
]);
$data_string = json_encode($data);
I need to connect to the Outbrain API (This is the documentation: http://docs.amplifyv01.apiary.io/#).
There's a minor example in there, but when I tried connecting to my own account I didn't manage to do so...
Can't understand if I put the wrong CURLOPT_URL or didn't write my credentials in the right form... This is my code:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.outbrain.com/amplify/v0.1/login");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: BASIC BASE-64-ENC(USERNAME:PASSWORD)",
"OB-TOKEN-V1: MY_ACCESS_TOKEN"
));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
If anyone knows why it didn't worked - I'd very much appreciate it...
Also if anyone has an additional code for talking with the Outbrain API - It'll help me a lot.
Thank you!
<?php
$outbrain_user = 'xxx';
$outbrain_pass = 'xxx';
// Basic access authentication
$enc_credentials = base64_encode($outbrain_user.':'.$outbrain_pass);
$ba_authenticacion = 'Basic '.$enc_credentials;
$auth_header = array(
'Authorization: '.$ba_authenticacion
);
$outbrain_api_endpoint = 'https://api.outbrain.com/amplify/v0.1/';
// authentication
$auth_url = $outbrain_api_endpoint.'/login';
$curl = curl_init();
curl_setopt_array($curl,
array(
CURLOPT_URL => $auth_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $auth_header
)
);
$json_access_token = curl_exec($curl);
// parsing json object to string
$token_object = json_decode($json_access_token);
$token_array = get_object_vars($token_object);
// api access_token
$access_token = $token_array['OB-TOKEN-V1'];
Basically you got a wrong syntax while parsing CURLOPT_HTTPHEADER array,
also outbrain uses a basic access authentication, you can check here for docs https://en.wikipedia.org/wiki/Basic_access_authentication.
With this code you can return the access_token from outbrain.
I tried integrating Azure ML API with PHP but unfortunately getting an error in response.
Updated: I have used request response API sending through json response
Below is the response obtained on executing PHP script:
array(1) { ["error"]=> array(3) { ["code"]=> string(11) "BadArgument"
["message"]=> string(26) "Invalid argument provided." ["details"]=> array(1)
{[0]=> array(2) { ["code"]=> string(18) "RequestBodyInvalid" ["message"]=>
string(68) "No request body provided or error in deserializing the request
body." } } } }
PHP Script:
$url = 'URL';
$api_key = 'API';
$data = array(
'Inputs'=> array(
'My Experiment Name'=> array(
"ColumnNames" => [['Column1'],
['Column2'],
['Column3'],
['Column4'],
['Column5'],
['Column6'],
['Column7']],
"Values" => [ ['Value1'],
['Value2'],
['Value3'],
['Value4'],
['Value5'],
['Value6'],
['Value7']]
),
),
'GlobalParameters' => new StdClass(),
);
$body = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer '.$api_key, 'Accept: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = json_decode(curl_exec($ch), true);
//echo 'Curl error: ' . curl_error($ch);
curl_close($ch);
var_dump ($response);
I have followed few examples, still unable to crack it. Please let me know the solution for this.
According to the error information, I think the issue was caused by requesting the ML REST API without correct json body.
I suggest that you can refer to the article "Getting started with the Text Analytics APIs to detect sentiment, key phrases, topics and language" to correctly format your input rows in JSON as the request body and try again.
Hope it helps.
If you can update your question for specifying which ML REST API you used, I think it's very helpful for figuring out the issue.
Expect your update.
I have to implement form on a webpage, that sends data to Microsoft Dynamics CRM when submited. The data needs to be saved to a certain lead.
I have created simple PHP script that uses curl to communicate with CRM server but I always get 401 status code that indicates authorization has failed.
define('MS_CRM_URL', 'https://______.crm.dynamics.com/');
define('MS_CRM_USER', 'user#domain.com');
define('MS_CRM_PASS', 'password');
$method = '/api/data/v8.0/accounts?$select=name&$top=3';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, MS_CRM_URL . $method);
curl_setopt($ch, CURLOPT_USERPWD, MS_CRM_USER .':'. MS_CRM_PASS);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept' => 'application/json',
'OData-MaxVersion' => '4.0',
'OData-Version' => '4.0',
'Content-Type' => 'application/json',
));
$server_output = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$json = array();
if ((int)$status_code === 200) {
$json = json_decode($server_output);
}
echo '<pre>';
var_dump($status_code);
var_dump($json);
echo '</pre>';
The $method var contents are taken from an example that I've found somewhere on Microsoft documentation site. The documentation was not very helpful to me.
Have you seen this yet? http://jlattimer.blogspot.com/2015/02/soap-only-authentication-using-php.html
Also, it looks like there's an ADAL library for PHP. https://github.com/jamesmcq/azure-activedirectory-library-for-php You should be able to authenticate using that.
You can do it using SOAP Authentication. I have same requirement where we need to send data to Dynamics 365 online from PHP website. I have achieved it using source code from below link :
https://bitbucket.org/nigelheap/msdynamicsphp-master