Linkedin Invitation API Internal server error - php

Yo!
I'm trying to use the linkedin invitation api to allow users to conncect on linkedin from my application using email-addresses. I am able to find people, access the api and so on. I can't get the invites to work though. I am using php (Laravel).
I based myself on the example from the linkedin documentation ( Linkedin Invite API ). I send my data in a post using JSON (that contains the same info as their example).
I ask permission to use w_messages, the post works and my variables contain the correct information. I get a Internal Server error as a result.
$data = array(
"recipients" => array(
"values" => array(
"person" => array(
"_path" => "/people/email=".$email,
"first-name" => $firstname,
"last-name" => $lastname
)
)
),
"subject" => "Bla",
"body"=> "BlaBLa",
"item-content" => array(
"invitation-request" => array(
"connect-type" => "friend"
)
)
);
$dataString = json_encode($data);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-type: application/json\r\n".
"Connection: close\r\n" .
"Content-length: " . strlen($dataString) . "\r\n",
'content' => $dataString
)
);
$params = array('oauth2_access_token' => Session::get('access_token'),
'format' => 'json'
);
$url = "https://api.linkedin.com/v1/people/~/mailbox".'?' . http_build_query($params);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
Log::info($result);
return Response::json(array("invite" => "sent"));
I assume I'm doing something wrong but don't really know where to look.

Looks like you doing this manually, have you tried using a tried & tested third party library like simple-linkedinphp - A PHP-based wrapper for the LinkedIn API.
https://code.google.com/p/simple-linkedinphp/wiki/Reference
Usage:
// Connect
$API_CONFIG = array(
'appKey' => '<your application key here>',
'appSecret' => '<your application secret here>',
'callbackUrl' => NULL
);
$linkedin = new LinkedIn($API_CONFIG);
// Send Invite
$linkedin->invite($method, $recipient, $subject, $body, $type = 'friend');
Doc: https://code.google.com/p/simple-linkedinphp/wiki/Reference

Related

FB Messanger bot - can not make sending messages work

I am making a FB messenger bot. I can not get the part with making the bot sending a message back to work.
I am using Heroku for the Webhook.
I am following this tutorial: https://www.youtube.com/watch?v=__SWFhHocDI
Here is my code (I changed the token variable, I do use the real token):
file_put_contents('fb.txt', file_get_contents('php://input'));
$fb = file_get_contents('fb.txt');
$fb = json_decode($fb);
$rid = $fb->entry[0]->messaging[0]->sender->id;
$token = "MYTOKEN";
$data = array(
'recipient' => array('id'=>"$rid"),
'message' => array('text'=>"Nice to meet you!")
);
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode($data),
'header' => "Content-Type: application/json\n"
)
);
$context = stream_context_create($options);
file_get_contents("https://graph.facebook.com/v2.8/me/subscribed_apps?access_token=$token", false, $context);
Can you find any problems? :-) Thank you for taking time.
maybe you should try something like this -
file_get_contents("https://graph.facebook.com/v2.6/me/messages?access_token=$token", false, $context);

Field "to" must be a JSON string [Firebase]

I'm trying to send notification using a cron job. I migrated from GCM to FCM. In my server side, I changed https://android.googleapis.com/gcm/send to https://fcm.googleapis.com/fcm/send and also updated on how to request the data changing registration_ids to to. Checking the json passed it is a valid json but I'm having an error Field "to" must be a JSON string. Is there anyway to solve this?
Here is my code
function sendNotificationFCM($apiKey, $registrationIDs, $messageText,$id) {
$headers = array(
'Content-Type:application/json',
'Authorization:key=' . $apiKey
);
$message = array(
'to' => $registrationIDs,
'data' => array(
"message" => $messageText,
"id" => $id,
),
);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'https://fcm.googleapis.com/fcm/send',
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => json_encode($message)
));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
Try explicitly converting $registrationIDs to string.
$message = array(
'to' => (string)$registrationIDs,
'data' => array(
"message" => $messageText,
"id" => $id,
),
);
Edited Answer
The 'to' parameter requires a string - which is the recipient of the message.
The $registrationIDs could be passed a separate parameter (as string array) to 'registration_ids'
Edit your code to something like this:
$recipient = "YOUR_MESSAGE_RECIPIENT";
$message = array(
'to' => $recipient,
'registration_ids' => $registrationIDs,
'data' => array(
"message" => $messageText,
"id" => $id,
),
);
Where $recipient is
a registration token, notification key, or topic.
Refer this:
Firebase Cloud Messaging HTTP Protocol
Try making to as a string:
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
...
},
}
// if you are using an array like
$fcm_ids = array();
$fcm_ids[] = "id_1";
$fcm_ids[] = "id_2";
$fcm_ids[] = "id_3";
//.
//.
//.
$fcm_ids[] = "id_n";
// note: limit is 1000 to send notifications to multiple ids at once.
// in your messsage send notification function use ids like this
$json_data = [
"to" => implode($fcm_ids),
"notification" => [
"title" => $title,
"body" => $body,
],
"data" => [
"str_1" => "something",
"str_2" => "something",
.
.
.
"str_n" => "something"
]
];
I had a similar problem. It turns out when I retrieved the Registration Token from my Firebase database (using this Firebase PHP Client), it was returned with double quotes at the beginning and at the end of the token. So I had to remove the first and last characters from the token before I could use it. The code below solved my problem
substr($registrationToken, 1, -1)

How to set request headers in WordPress Rest API calls

Hi am new to WP development, I would like to add request headers to WordPress Rest API calls but don't know how, can anyone help me in this?
I tried following code but no luck
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( Fk-Affiliate-Id . ':' . YOUR_PASSWORD )
)
);
$api_url = 'https://affiliate.com/api/';
global $affiliate;
$response = wp_remote_request( add_query_arg( array(
'Affiliate-Id' => $affiliate['aff-id'],
'Affiliate-Token' => $affiliate['aff-token']
), $api_url ) , array( 'timeout' => 10));
You can send the headers you need by including those in request options - second argument of wp_remote_request function($args in my example)
$args = [
'method' => 'GET',
'timeout' => 10,
'headers' => array() //add headers here
];
wp_remote_request('http://test.com', $args);

Update video using youtube api

I want to update a video using google api v3 and i get the error 400 Bad Request.
This is my code.
$url = 'https://www.googleapis.com/youtube/v3/videos?part=snippet&videoId='.$_GET['videoId'].'&access_token='.Session::get('access_token');
$params = array(
"id"=> $_GET['videoId'],
"kind"=> "youtube#video",
'snippet' => array(
"title"=> "I'm being changed.",
"categoryId"=> "10",
"tags"=> array(
"humanities",
"Harpham",
"BYU"
),
'description' => 'test!'
)
);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'PUT',
'content' => http_build_query($params),
),
);
$context = stream_context_create($options);
$result = json_decode(file_get_contents($url, false, $context));
I think since you don't set all parameters inside snippet, that's giving an error. What you can do is, first getting that video with videos->list, then updating the field you are interested in and sending back the update request with the whole object back.
Here's an example also utilizing php client library: https://github.com/youtube/api-samples/blob/master/php/update_video.php

Paypal sandbox PDT Fail 4020

So, I've created a sandbox transaction and am trying to get the data for it on my thank you page.
<?php
$tx = $_REQUEST['tx'];
$pdti = "REDACTED";
$url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
$data = array("tx" => $tx, "at" => $pdti,"cmd" => "_notify-synch");
$options = array(
"http" => array(
"header" => "Content-type: application/x-www-form-urlencoded\r\n",
"method" => "POST",
"content" => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url,false,$context);
var_dump($result);
?>
But when I go to thankyoupage.php?tx=ID, where 'ID' is the transaction ID, I get the following:
string(16) "FAIL Error: 4020"
I can't find any documentation that details what this error means, and am completely stuck, so I'd be grateful for any feedback.
Most probably your Authentication Token ($pdti) is incorrectly entered.

Categories