We are using dynamics crm 2016 in-premise using IFD. I am using below code to get a lead from CRM using API in php
//$url = $crm_url . "XRMServices/2011/OrganizationData.svc/LeadSet($lead)";
$url = $crm_url . "api/data/v8.1/leads($lead)";
$headers = array(
'Method: GET',
'OData-MaxVersion: 4.0',
'OData-Version: 4.0',
'Connection: keep-alive',
'User-Agent: PHP-SOAP-CURL',
'Content-Type: application/json; charset=utf-8',
'Accept: application/json',
'Host: ' . $host);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
if( $response === false) { echo 'Curl error: ' . curl_error($ch);}
$status = curl_getinfo( $ch, CURLINFO_HTTP_CODE );echo "Status: $status ";
$response=json_decode($response, true);
foreach ($response as $id) {
$guid = $id['LeadId'];
echo 'GUID: ' . $guid. ' ';
}
echo "Lead id: "; var_dump($response['LeadId']);
echo "<pre>";
print_r($response);
echo "</pre>";
curl_close($ch);
I am getting this response:
Status: 401 GUID: T GUID: GUID: Lead id: NULL
Array
(
[Message] => There was an error processing the request.
[StackTrace] =>
[ExceptionType] =>
)
Can someone please help me how to get the correct response.
Alse please let me know if there is another method.
Many thanks..
Seems like the leadid is not filled in your request.
$url = $crm_url . "api/data/v8.1/leads($lead)";
Did you replace that variable by the guid?
exemple:
https://contoso.api.crm.dynamics.com/api/data/v8.1/accounts(1e3983e8-7894-e511-80db-3863bb2e3248)
Also, a good trick to test your queries is to paste them into your browser. I use Chrome. With IE you need to disable the RSS feed.
Sorry, i'm not familliar with php.
Related
I'm trying to shorten URL using the bit.ly API V4, but i always get the same error, I can't get the short URL.
Can someone help me please?
Here is the code that Im using:
$long_url = 'https://estaticos.muyinteresante.es/media/cache/1140x_thumb/uploads/images/gallery/59bbb29c5bafe878503c9872/husky-siberiano-bosque.jpg';
$apiv4 = 'https://api-ssl.bitly.com/v4/bitlinks';
$genericAccessToken = 'MyToken';
$data = array(
'long_url' => $long_url
);
$payload = json_encode($data);
$header = array(
'Authorization: Bearer ' . $genericAccessToken,
'Content-Type: application/json',
'Content-Length: ' . strlen($payload)
);
$ch = curl_init($apiv4);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
$resultToJson = json_decode($result);
if (isset($resultToJson->link)) {
echo $resultToJson->link;
}
else {
echo 'Not found';
}
I always get "Not found", anybody knows why?
I have a very detailed answer on integrating with Bit.ly V4 and how to shorten URLs.
You can find the link here.
So I've been working on a login system with Discord, and this is the first roadblock I've hit.
I'm trying to POST to /oauth2/token/revoke and it keeps giving me back the error "invalid_client".
I've tried using a client secret and not using it, only sending the token, changing the name "access_token" to "token", and a few other things I can't recall.
My code for sending the request is this:
session_start();
//debug thing
echo OAUTH2_CLIENT_ID;
$params = array(
"access_token" => $_SESSION["access_token"],
"client_id" => OAUTH2_CLIENT_ID
);
apiRequest("https://discordapp.com/api/oauth2/token/revoke", $params);
The code for that apiRequest function is adapted from a different thread on here and is as follows:
function apiRequest($url, $post=FALSE, $headers=array()) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
if($post) {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
}
$headers[] = 'Accept: application/json, application/x-www-form-urlencoded';
if(isset($_SESSION["access_token"]))
$headers[] = 'Authorization: Bearer ' . $_SESSION["access_token"];
// using this to see my headers
var_dump($headers);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code != 200) {
//using this to see the error
echo $response;
exit();
fatalError($code, $_SERVER[REQUEST_URI]);
}
return $response;
}
And yes, both the access token and client ID are valid. They work properly on other requests and I've displayed them on this page and confirmed them to be valid.
Any ideas? Am I missing something stupid?
Worked out the fields you need and fiddled with curl a bit and this code is working.
$params = array(
"token" => $_SESSION["access_token"],
"client_id" => OAUTH2_CLIENT_ID,
"client_secret" => OAUTH2_CLIENT_SECRET
);
$ch = curl_init("https://discord.com/api/oauth2/token/revoke");
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$headers[] = 'Accept: application/json';
$headers[] = 'Authorization: Bearer ' . $_SESSION["access_token"];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code != 200) {
#error handling
}
curl_close($ch);
I'm trying to POST an image on Mastodon (specifically on Humblr) but I cannot get the media_id, the response is null, but I'm not sure where the problem is.
I can publish text, no problem, so the authentication part is fine, I only have the problem with the image, the only difference I can see in the documentation is that "Media file encoded using multipart/form-data".
Here's my code so far...
$headers = ['Authorization: Bearer '.$settings['access_token'] , 'Content-Type: multipart/form-data'];
$mime_type = mime_content_type($urlImage);
$cf = curl_file_create($urlImage,$mime_type,'file');
$media_data = array( "file" => $cf);
$ch_status = curl_init();
curl_setopt($ch_status, CURLOPT_URL, "https://humblr.social/api/v1/media");
curl_setopt($ch_status, CURLOPT_POST, 1);
curl_setopt($ch_status, CURLOPT_POSTFIELDS, $media_data);
curl_setopt($ch_status, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_status, CURLOPT_HTTPHEADER, $headers);
$media_status = json_decode(curl_exec($ch_status));
echo "Response: ".json_encode($media_status);
From this I want to extract the $media_status-> media_id
I don't really know much about 'multipart/form-data' to be honest.
Am I missing something?
This took some trial and error for me as well. Here's what worked for me (PHP 7.3):
$curl_file = curl_file_create('/path/to/file.jpg','image/jpg','file.jpg');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://instanceurl.com/api/v1/media');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer YOUR_TOKEN_HERE',
'Content-Type: multipart/form-data'
]);
$body = [
'file' => $curl_file
];
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$response = curl_exec($ch);
if (!$response) {
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}
echo 'HTTP Status Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
echo 'Response Body: ' . $response . PHP_EOL;
curl_close($ch);
I'm trying to send this POST request and it won't echo any result,
I've tried to use different techniques but it's either giving me 500 error or just stuck on a blank page, whereas it should echo back at least something like this:
{"errorCode":"APP-0001","message":"Something really bad happened. Please contact your administrator, right now!","fields":{},"operationMessage":"Something really bad happened. Please contact your administrator, right now!"}
<?php
$countryCode = $_POST['countryCode'];
$phoneNumber = $_POST['phoneNumber'];
$data = array("countryCode" => "$countryCode", "phoneNumber" => "$phoneNumber");
$data_string = json_encode($data);
$ch = curl_init('https://consumer-edge-service.careem.com/api/v8/user/partialSignup?device=ACMA');
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(
'Authorization: SCBOHw6OOZD1lOJyS2dz',
'Agent: ACMA',
'User-Agent: ACMA/8.7.7',
'Device: ' . $phoneNumber,
'Provider-Access-Key: 6ba82ffa',
'Version: 8772',
'x-careem-position: 29.452370,32.752543,1799.999023,1552014765404',
'Content-Type: application/json; charset=UTF-8',
'Content-Length: 46',
'Host: consumer-edge-service.careem.com',
'Connection: close',
'Accept-Encoding: gzip, deflate',
'X-NewRelic-ID: VQMAWFVVABAHVVBXDwgFVA==')
);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
echo $result;
?>
I have a sand box account in fortnox and i am trying to get the access token using following code, but i keep getting same error:
$requestMethod = "GET";
$ch = curl_init();
$options = array(
'Authorization-Code: '. AUTHORIZATION_CODE .'',
'Client-Secret: '. CLIENT_SECRET .'',
'Content-Type: '. CONTENT_TYPE .'',
'Accept: '. ACCEPTS .''
);
curl_setopt ($ch, CURLOPT_CAINFO, "/xampp/htdocs/cacert.pem");
curl_setopt($ch, CURLOPT_URL, "https://api.fortnox.se/3/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, $options);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $requestMethod);
$curlResponse = curl_exec($ch);
$info = curl_getinfo($ch);
//echo 'Took ' . $info['total_time'] . ' seconds for url ' . $info['url'];
echo $curlResponse;
if ($curlResponse === FALSE) {
echo "cURL Error: " . curl_error($ch);
}
curl_close($ch);
Error: PHP / 4.5.37 Can not sign in , access tokens , or client- secret missing ( 2000311 ) .
I get the Access-Token using the following code which is very close to your code. It worked fine for me.
Your error is described on the Errors page, but it adds nothing new to the text you already shown.
Please note that you can retrive the Access-Token only once, check Authentication section in the Fortnox documentation.
An Access-Token can only be retrieved once with every Authorization-Code, multiple requests with the same Authorization-Code will make both the Authorization-Code and the Access-Token invalid.
public function actionRetrieveAccessToken($authCode, $clientSecret)
{
$headers = [
'Authorization-Code: ' . $authCode,
'Client-Secret: ' . $clientSecret,
'Content-Type: application/json',
'Accept: application/json',
];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api.fortnox.se/3/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$responseText = curl_exec($curl);
$response = json_decode($responseText, true);
if (isset($response['ErrorInformation'])) {
echo 'Request failed with error messages:' . PHP_EOL;
echo $response['ErrorInformation']['Error'] . ': ';
echo $response['ErrorInformation']['Message'];
echo ' (' . $response['ErrorInformation']['Code'] . ')' . PHP_EOL;
} else {
echo 'Access Token: ' . $response['Authorization']['AccessToken'] . PHP_EOL;
}
}
$content_type = 'application/json';
$api_url="api url here";
$curl = curl_init();
$headers = array(
'Content-Type: '.$content_type.'',
'Authorization : '.'Bearer ' . $this->access_token
);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
if (!empty($data))
{
$data = json_encode($data);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
$response = curl_exec($curl);
logthis($response,"utils");
curl_close($curl);