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);
Related
I am trying to upload a video using LinkedIn API V2 but I am unable to post successfully video to my LinkedIn Individual Account.
Please help.
Returning Below Response from LinkedIn API:
SignatureDoesNotMatch
The request signature we calculated does not match the signature you provided. Check your key and signing method.
$person_id=LINKEDIN_ACCOUNT_ID;
$access_token= LINKEDIN_ACCESS_TOKEN;
$share_text='Video Upload and Share Text';
$author = "urn:li:person:".$person_id;
$r_url='https://api.linkedin.com/v2/assets?action=registerUpload';
$r_params = array(
'registerUploadRequest'=>array(
'recipes'=>array('urn:li:digitalmediaRecipe:feedshare-video'),
'owner' => $author,
)
);
$handle = curl_init();
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($handle, CURLOPT_URL, $r_url);
curl_setopt($handle, CURLOPT_VERBOSE, FALSE);
$header = array();
$header[] ='Authorization : Bearer '.$access_token;
$header[] = 'Content-Type: application/json; charset=UTF-8';
curl_setopt($handle, CURLOPT_HTTPHEADER, $header);
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($r_params));
$json1 = curl_exec($handle);
$json1=json_decode($json1,true);
if($json1['value']['uploadMechanism']['com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest']['uploadUrl']){
$target_url=$json1['value']['uploadMechanism']['com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest']['uploadUrl'];
$return_header=$json1['value']['uploadMechanism']['com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest']['headers'];
$parts = parse_url($target_url);
parse_str($parts['query'], $query);
$amz_signature=$query['X-Amz-Signature'];
$target_header=array();
$target_header[]='Host: video-uploads-prod.s3-accelerate.amazonaws.com';
$target_header[]="Content-Type:".trim($return_header['Content-Type']);
$target_header[]="x-amz-server-side-encryption:".trim($return_header['x-amz-server-side-encryption']);
$target_header[]='x-amz-server-side-encryption-aws-kms-key-id:'.trim($return_header['x-amz-server-side-encryption-aws-kms-key-id']);
$video_path = DIR_PATH_TO_VIDEO_FILE.'example_video.mp4';
$post_data=array('file'=>$video_path);
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $target_header);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($video_path));
$json2=curl_exec ($ch);
curl_close ($ch);
$json2=json_decode($json2,true);
$media_id=str_replace('urn:li:digitalmediaAsset:','', $json1['value']['asset']);
$return_data=array();
$check_url = 'https://api.linkedin.com/v2/assets/'.$media_id;
$handle = curl_init();
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($handle, CURLOPT_HEADER, FALSE);
curl_setopt($handle, CURLOPT_URL, $check_url);
$header = array();
$header[] ='Authorization : Bearer '.$access_token;
$header[] = 'Content-Type: application/json; charset=UTF-8';
curl_setopt($handle, CURLOPT_HTTPHEADER,$header);
$return_data= curl_exec($handle);
$return_data= json_decode($return_data,true);
$author = "urn:li:person:".$person_id;
$post_url = 'https://api.linkedin.com/v2/ugcPosts';
$media_data=array();
$media_data[0]=array(
'status'=>'READY',
'description'=>array('text'=>'Official LinkedIn Blog'),
'media'=>$media_id,
'title'=>array('text'=>"Official LinkedIn Blog"),
);
$params = array(
'author' => $author,
'lifecycleState' => 'PUBLISHED',
'specificContent' => array(
'com.linkedin.ugc.ShareContent' => array(
'shareCommentary' => array(
'text' => "Video media set in post",
),
'shareMediaCategory' => 'VIDEO',
'media'=>$media_data,
'originalUrl'=>'https://www.google.com'
)
),
'visibility' => array(
'com.linkedin.ugc.MemberNetworkVisibility' => 'PUBLIC'
)
);
$handle = curl_init();
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($handle, CURLOPT_URL, $post_url);
curl_setopt($handle, CURLOPT_VERBOSE, FALSE);
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($params));
$header = array();
$header[] ='Authorization : Bearer '.$access_token;
$header[] = 'Content-Type: application/json; charset=UTF-8';
$header[] = 'X-Restli-Protocol-Version:2.0.0';
curl_setopt($handle, CURLOPT_HTTPHEADER, $header);
$json3 = curl_exec($handle);
$json3=json_decode($json3);
I need to upload video post to LinkedIn Account successfully but I am unable to understand that from LinkedIn documentation too. I have tried so much but not succeed.
Please someone who has successfully uploaded a video with V2 then please help.
Hi linkedin not released video uploads yet.You can use the article EP( "shareMediaCategory": "ARTICLE") to send videos to linkedin
I make use of the LinkedIn API from Zoonman to do the client post request, but this is out of the scope of the question.
Because i could not get the php curl functions to work properly, i am using the command line interface to do the request, and it works! See my code below.
BUT. even tough the upload works. When i do a request to get the status of the upload, it is still "WAITING_UPLOAD". So i think #augustine jenin is right, that it is not supported yet. (may 2019)
<?php
// first register upload
$data = [
"registerUploadRequest" => [
"recipes" => [
"urn:li:digitalmediaRecipe:feedshare-video"
],
"owner" => "urn:li:organization:" . $liPageId,
"serviceRelationships"=> [
[
"relationshipType"=> "OWNER",
"identifier" => "urn:li:userGeneratedContent"
]
]
]
];
$register = $client->post('assets?action=registerUpload', $data);
// get upload url and header
$uploadUrl = $register["value"]["uploadMechanism"]["com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest"]["uploadUrl"];
$headers = $register["value"]["uploadMechanism"]["com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest"]["headers"];
$curlHeaders = "";
foreach($headers as $htype => $header) {
$curlHeaders .= ' -H "' . $htype . ':' . $header . '"';
}
// go upload the image to the url
$filePath = "/path/to/your/file";
$command = '/usr/bin/curl -v';
$command .= $curlHeaders;
$command .= ' --upload-file \'' . $filePath . '\' \'' . $uploadUrl . '\'';
// try it yourself by running this on the command line
//echo $command;
shell_exec($command);
?>
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.
I need to insert an id-variable into the link "https://api.quickpay.net/payments/9727866/link" (instead of "9727866") but i can't seem to get the syntax right.
It doesn't seem to accept my variables no matter what I do.. anyone knows how to do this?
$params = array(
"amount" => 100,
"order_id" => 999999
);
$data_string = http_build_query($params, '&');
$headers = array(
'Accept-Version: v10',
'Accept: application/json',
'Authorization: Basic ' . base64_encode(":HIDDEN_API_KEY")
);
$ch = curl_init('https://api.quickpay.net/payments/9727866/link');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if(!curl_exec($ch)){
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}
curl_close($ch);
print_r($result);
It's probably because you're using single quotes. Try any of the following:
$ch = curl_init("https://api.quickpay.net/payments/$variable/link");
or
$ch = curl_init("https://api.quickpay.net/payments/{$variable}/link");
or
$ch = curl_init("https://api.quickpay.net/payments/" . $variable . "/link");
or
$str = "https://api.quickpay.net/payments/" . $variable . "/link";
$ch = curl_init($str);
or
sprintf("https://api.quickpay.net/payments/%s/link", $variable);
Whatever floats your boat.
I am trying to create the proper signature for the Ascentis API. There documentation is http://www.ascentis.com/API/Ascentis_API_Documentation.pdf. Page 4 describes the Signature format.
Here is my PHP code. Am I doing something wrong? I get a "not authorized error".
$url='https://selfservice2.ascentis.com/mycompany/api/v1.1/employees';
$timestamp=gmdate('Y-m-d\TH:i:s\Z');
$path=strtolower(str_replace('https://selfservice2.ascentis.com','',$url));
$signature_string="GET {$path} {$timestamp}";
$signature=hash_hmac("sha1",$signature_string,$secret_key);
$authorization=encodeUrl($client_key).':'.encodeUrl($signature);
Here's a more complete example. Props to #Steve Lloyd for getting me in the right direction.
$codes['secret_key'] = 'my_secret';
$client_key = 'my_key';
$url = 'https://selfservice.ascentis.com/my_company/api/v1.1/employees?lastname=%s';
$timestamp = gmdate('Y-m-d\TH:i:s\Z');
$path = strtolower(str_replace('https://selfservice.ascentis.com', '', $url));
$signature_string = "GET {$path} {$timestamp}";
$signature = base64_encode(hash_hmac("sha1", $signature_string, $codes['secret_key'], TRUE));
$authorization = urlencode($client_key) . ':' . urlencode($signature);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: " . $authorization,
"Accept: application/xml",
"Timestamp: " . $timestamp,
]);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$data = curl_exec($ch);
$info = curl_getinfo($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "Received $httpcode from $url\n";
echo "Key: $client_key\n";
echo "Signature: $signature\n";
echo "authorization: $authorization\n";
echo "request info:" . var_export($info, TRUE) . "\n";
echo "data:\n";
var_export($data, TRUE)
After playing the trial and error game I was able to get this working. It turns out that you have to set hash_hmac to raw_output. Here is the working code:
$url='https://selfservice2.ascentis.com/mycompany/api/v1.1/employees';
$timestamp=gmdate('Y-m-d\TH:i:s\Z');
$path=strtolower(str_replace('https://selfservice2.ascentis.com','',$url));
$signature_string="GET {$path} {$timestamp}";
$signature=base64_encode(hash_hmac("sha1",$signature_string,$codes['secret_key'],true));
$authorization=encodeUrl($client_key).':'.encodeUrl($signature);
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);