LinkedIn:Media posts not visible on wall after sharing using REST API - php

I am trying to create a PHP script which shares posts on LinkedIn automatically with the help of zoonman/linkedin-api-php-client SDK https://github.com/zoonman/linkedin-api-php-client. I could share a post without any media and it is visible on my LinkedIn wall. When trying to share a post with an image, the API response returned true with an id urn:li:share:6605357601301553152, but the post is not visible on my wall. I am providing my code for posting media below.
public function postMedia($data,$accessToken){
try{
$mediareturn = $this->client->post(
'assets?action=registerUpload',
[
"registerUploadRequest"=>[
"recipes"=> [
"urn:li:digitalmediaRecipe:feedshare-image"
],
"owner"=> "urn:li:person:**********",
"serviceRelationships"=> [
[
"relationshipType"=> "OWNER",
"identifier"=> "urn:li:userGeneratedContent"
]
]
]
]
);
$uploadUrl = $mediareturn['value']['uploadMechanism']['com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest']['uploadUrl'];
$filepath = $this->root_url.'assets/uploads/'.$data['attachment'];
$shell = shell_exec('curl -i --upload-file '.$filepath.' --header "Authorization: Bearer "'.$accessToken.' '.$uploadUrl);
$share = $this->client->post(
'ugcPosts',
[
"author"=> "urn:li:person:**********",
"lifecycleState"=> "PUBLISHED",
"specificContent"=> [
"com.linkedin.ugc.ShareContent"=> [
"shareCommentary"=> [
"text"=> $data['content']
],
"shareMediaCategory"=> 'IMAGE',
"media"=> [
[
"status"=> "READY",
"description"=> [
"text"=> "Center stage!"
],
"media"=> $mediareturn['value']['asset'],
"title"=> [
"text"=> "Test posting"
],
"thumbnails"=> [],
]
]
]
],
"visibility"=> [
"com.linkedin.ugc.MemberNetworkVisibility"=> "PUBLIC"
]
]
);
return $share;
}catch(Exception $e){
throw $e;
}
}
The above code was written referring the official document https://learn.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/share-on-linkedin.
Any and all help would be greatly appreciated.

Related

How we can insert header and footer in google docs with google docs api using PHP code

I want to insert header and footer in my google docs with google docs api in PHP code. I am doing it like this-
$requests = new Google_Service_Docs_Request(array(
'createHeader' => [
'type' => 'TITLE',
'sectionBreakLocation' => [
'index' => 0
],
],
)),
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
'requests' => $requests
));
$response = $service->documents->batchUpdate($documentId, $batchUpdateRequest);
but, i am getting this error-
PHP Fatal error: Uncaught Google\Service\Exception: {
"error": {
"code": 400,
"message": "Invalid value at 'requests[5].create_header.type' (type.googleapis.com/google.apps.docs.v1.HeaderFooterType), \"TITLE\"",
"errors": [
{
"message": "Invalid value at 'requests[5].create_header.type' (type.googleapis.com/google.apps.docs.v1.HeaderFooterType), \"TITLE\"",
"reason": "invalid"
}
],
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "requests[5].create_header.type",
"description": "Invalid value at 'requests[5].create_header.type' (type.googleapis.com/google.apps.docs.v1.HeaderFooterType), \"TITLE\""
}
]
}
]
}
}
Please help me out with this, That how we can insert texts in header and footer in google docs using PHP.
In your script, how about the following modification?
Create header:
I thought that the reason of the error message of Invalid value at 'requests[5].create_header.type' (type.googleapis.com/google.apps.docs.v1.HeaderFooterType), \"TITLE\"" is due to 'type' => 'TITLE',. But when I saw your script, $requests is required to be an array. So how about the following modification?
From:
$requests = new Google_Service_Docs_Request(array(
'createHeader' => [
'type' => 'TITLE',
'sectionBreakLocation' => [
'index' => 0
],
],
)),
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
'requests' => $requests
));
To:
$requests = new Google_Service_Docs_Request(array(
'createHeader' => [
'type' => 'DEFAULT',
'sectionBreakLocation' => [
'index' => 0
],
],
));
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
'requests' => array($requests)
));
Create footer:
In this case, please replace createHeader to createFooter in the above $requests.
Note:
As additional information, when you want to use the first page header and footer, you can use the following request.
$requests = new Google_Service_Docs_Request(array(
'updateDocumentStyle' => [
'documentStyle' => [
'useFirstPageHeaderFooter' => true,
],
'fields' => 'useFirstPageHeaderFooter',
],
));
References:
CreateHeaderRequest
CreateFooterRequest

Convert cURL to Guzzle Post sending json data

I have this cURL command that I need to convert to PHP using Guzzle 7. I've have been researching this for a few (well, more than a few) days and getting nowhere fast. The cURL command uses the Simpli.fi api to create an organization under the parent org.
curl -i -X POST -H "X-App-Key: xxxx-xx-xx-xx-xxxxxx" -H "X-User-Key: xxxx-xx-xx-xx-xxxxxx" \
-H "Content-Type: application/json" \
-d '{
"organization": {
"name": "My New Organization",
"parent_id": "5786",
"custom_id": "<Put your organization identifier here or omit this optional field>"
}
}' \
"https://app.simpli.fi/api/organizations"
I was able to convert it using this website but it doesn't use Guzzle: https://onlinedevtools.in/curl
Here's what it gave me:
include('vendor/rmccue/requests/library/Requests.php');
Requests::register_autoloader();
$headers = array(
'X-App-Key' => 'xxxx-xx-xx-xx-xxxxxx',
'X-User-Key' => 'xxxx-xx-xx-xx-xxxxxx',
'Content-Type' => 'application/json'
);
$data = '{\n "organization": {\n "name": "My New Organization",\n "parent_id": "5786",\n "custom_id": "<Put your organization identifier here or omit this optional field>"\n }\n }';
$response = Requests::post('https://app.simpli.fi/api/organizations', $headers, $data);
Here's what I've tried so far aside from the converted code above:
public static function createOrganization()
{
self::init();
$client = new Client(['debug' => true]);
$request = $client->request('POST',
self::$baseUrl.'/organizations', [
'multipart' => [
[
'name' => 'data',
'contents' => "{'organization':{'name':'Pete's Pet Pagoda','parent_id':'1052385'}}",
],
],
'headers' => [
'x-app-key' => self::$appKey,
'x-user-key' => self::$userKey,
'Content-Type' => 'application/json',
]
]);
dd($response = $request->getStatusCode());
}
I'm getting quite a few different errors however this is the latest:
curl_setopt_array(): cannot represent a stream of type Output as a STDIO FILE*
Can anyone tell me what I'm doing wrong? Is there something missing?
UPDATE: After further research into this issue and chatting with a developer on the Laracast Slack channel, I've come to learn that this is a bug with the ['debug' => true] option when running on a Windows system and is described on this GITHUB page: https://github.com/guzzle/guzzle/issues/1413
I'm running on a Windows 10 Pro system. I corrected it on my end by changing the debug option to use fopen() like this:
'debug' => fopen('php://stderr', 'w'),
I use PHPStorm. It suggested using the 'wb' to make it binary safe. After changing it, the post requests worked fine.
You need to use body, not multipart. You can also use json.
$request = $client->request('POST',
self::$baseUrl.'/organizations', [
'headers' => [
'x-app-key' => self::$appKey,
'x-user-key' => self::$userKey,
'Content-Type' => 'application/json',
],
'body' => [
'{"organization":
[
{
"name":"Pete`s Pet Pagoda",
"parent_id":"1052385"
}
]
}',
],
]);
method 2
You can pass array to the json request option and it will convert it to json when sending the guzzle request. No need to use header as application/json as it applies internally.
$client->request('POST',
self::$baseUrl.'/organizations', [
'headers' => [
'x-app-key' => self::$appKey,
'x-user-key' => self::$userKey,
'Content-Type' => 'application/json',
],
'json' => [
"organization" => [
[
"name" => "Pete`s Pet Pagoda"
"parent_id" => "1052385"
]
]
],
]);
I hope this will help you. For further debugging use Middleware::tap(find more help here middleware+json+request)
try{
$client = new Client();
$response = $client->request('POST', self::$baseUrl.'/organizations',
[
'headers' => [
'x-app-key' => self::$appKey,
'x-user-key' => self::$userKey,
'Content-Type' => 'application/json',
],
'json' => [
'organization' =>
[
"name" => "some_name_value",
"parent_id" => "id_here",
"custom_id" => "custom id here"
]
]
]);
$_response = json_decode($response->getBody()->getContents(), true);
}
catch(BadResponseException $e){
$response = $e->getResponse();
$errorArray = json_decode($response->getBody()->getContents(), true);
//echo "<pre>";
//print_r($errorArray);
//return some message from errorarray;
}

i can't nested add in elasticsearch

i have a problem with elasticsearch
when i send request for add nested object with elasticseach script in postman it work but when i wanna send request for add new object in elasticseach , All objects in my index disappear.
it not work , this eliminates all other objects in elasticsearch and only add itself:
$params = [
'script' => [
'source' => 'ctx._source.products.add(params.products)',
'lang'=>'painless',
'params' =>['id'=>1,'name'=>'john']
]
];
$http = new Client();
$result = $http->request('post','http://my_server/new_frotel/_update/123', [
'body' =>$params,
'headers'=>[
'content-type'=>'application/json'
]
])
but it work in postman
POST : http://my_server/new_frotel/_update/123
{
"script": {
"source": "ctx._source.products.add(params.products)",
"lang": "painless",
"params": {
"products": {
"id":1,
"name":"john"
}
}
}
}

How do I match entries in the elasticsearch containing dashes (-)

ElasticSearch tokenizes the data.
So "this-that" gets split into 2 tokens.
If it makes a difference, I am using the 6.6 version of ES.
I have documents, that have different fields, such as title, descriptions, name, etc.
In order for us to have a unique identifier, the text in Title "This that" get slugified into "this-that".
I am trying to to query for "this-that" that would return that one document.
I have tried all sorts of things. I tried suggestions from other questions in this forum, as well as instructions in the https://www.elastic.co/guide/en/elasticsearch/reference/current/analyzer.html page.
Unfortunately, nothing seems to work.
Your help would be greatly appreciated.
Thank you in advance.
https://www.elastic.co/guide/en/elasticsearch/reference/current/analyzer.html
<?php
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$hosts = ['localhost:9200'];
$client = ClientBuilder::create()
->setHosts($hosts)
->build();
$params = array();
$params = [
'index' => 'shows',
'type' => '_doc',
'body' => [
'size'=> 10000,
'query' => [
'bool' => [
'must' => [ 'match' => [ 'name' => 'this-that'] ],
]
]
]
];
$response = $client->search($params);
print_r($response);
?>
There are no errors, but it finds all instances with the word "this" and the word "that" in the name field.
I would like to get just the one document back!
You can experiment with analyzers and tokenizers using Kibana console or http:
curl -XPOST "http://localhost:9200/_analyze" -H 'Content-Type: application/json' -d'{ "analyzer": "standard", "text": "this-that"}'
{
"tokens" : [
{
"token" : "this",
"start_offset" : 0,
"end_offset" : 4,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "that",
"start_offset" : 5,
"end_offset" : 9,
"type" : "<ALPHANUM>",
"position" : 1
}
]
}
curl -XPOST "http://localhost:9200/_analyze" -H 'Content-Type: application/json' -d'{ "analyzer": "keyword", "text": "this-that"}'
{
"tokens" : [
{
"token" : "this-that",
"start_offset" : 0,
"end_offset" : 9,
"type" : "word",
"position" : 0
}
]
}
To have always exact match for field, you must use keyword-tokenization. You can do it like this:
PUT test-index
{
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "keyword"
}
}
}
}
Which is exactly same as defining field as a keyword type to begin with:
PUT test-index2
{
"mappings": {
"properties": {
"name": {
"type": "keyword"
}
}
}
}

Facebook messenger template list not working on messenger app

Please I need some help......am developing a Facebook messenger chatbot and I have issue with the List template. When I send the template, the response is received and displays well on the web but on the messenger app, everything gets lumped in one button. Any idea will be appreciated. Thanks.
if($message == "about"){
$answer = ["attachment"=>[
"type"=>"template",
"payload"=>[
"template_type"=>"list",
"elements"=>[
[
"title"=> "Founder",
"image_url"=> "https://www.cloudways.com/blog/wp-content/uploads/Migrating-Your-Symfony-Website.jpg",
"subtitle"=> "Frederick Angel",
"default_action"=> [
"type"=> "web_url",
"url"=> "https://www.cloudways.com/blog/migrate-symfony-from-cpanel-to-cloud-hosting/",
"webview_height_ratio"=> "tall",
// "messenger_extensions"=> true
// "fallback_url"=> "https://peterssendreceiveapp.ngrok.io/"
],
"buttons"=>[
[
"type"=>"postback",
"title"=>"Read about him",
"payload"=>"Frederick Angel",
"webview_height_ratio"=> "tall",
// "messenger_extensions"=>true
],
]
],
[
"title"=>"Concepts?",
"default_action"=> [
"type"=> "web_url",
"url"=> "https://www.cloudways.com/blog/migrate-symfony-from-cpanel-to-cloud-hosting/",
"webview_height_ratio"=> "tall",
// "messenger_extensions"=> true
// "fallback_url"=> "https://peterssendreceiveapp.ngrok.io/"
],
"buttons"=>[
[
"type"=>"postback",
"title"=>"Read about it",
"payload"=>"concepts",
"webview_height_ratio"=> "tall"
],
]
]
]
]];
$template = [
'recipient' => [ 'id' => $senderId ],
'message' => $answer
];
}

Categories