Can't post to LinkedIn via API - php

here is my code:
$url = 'https://api.linkedin.com/v1/people/~/shares?oauth2_access_token=XXXXXXX';
$url .= '&format=json';
$postdata = http_build_query(
array(
'comment' => 'comment',
'content' => array('title' => 'LInkedIN title',
'description' => 'linkedin desc',
'submitted-url' => "https://developer.linkedin.com",
),
'visibility' => array('code' => 'anyone')
)
);
$opts = array('http' =>
array('method' => 'POST',
'header'=> "x-li-format : json\r\n".
"Host: api.linkedin.com\r\n".
"Content-Length: ".strlen($postdata)."\r\n".
"X-Target-URI: https://api.linkedin.com\r\n".
"Content-Type: application/json\r\n".
"Connection: Keep-Alive",
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
but there is no effect. Tried this service https://apigee.com/console/linkedin - works perfectly...
What's wrong?

what are you trying to build? the jssdk can do this much better! Check out this tool I developed:
http://datopstech.com/linkedin-share-tool/
and the source code:
https://github.com/kaburkett/LinkedIn-Advanced-Share
And base your project off that if you can.

Related

PHP Retrieve the headers of a response to a POST request a key-value pair

I have this php scripts:
$url = "https://mysite/web/session/authenticate";
$data = array (
'params' =>
array (
'db' => 'mydb',
'login' => 'myuserid',
'password' => 'myuserpwd',
),
);
$data = json_encode($data);
$options = array(
'http' => array(
'header' => "Content-Type: application/json\r\n"
. "Content-Length: " . strlen($data) . "\r\n",
'method' => 'POST',
'content' => $data,
)
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
From the server response I need to retrieve a key-value pair (set-cookie) contained in the headers.
Response fron POSTMAN
How can I do?
Thanks

file_get_contents request with Authorization

I want send a get request with file_get_contents. This code returns failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found.
How can I solve it?
<?php
define('API_KEY','eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...');
$data = http_build_query(
array(
'name' => 'test',
'count' => '1000',
)
);
$url = 'https://www.example.com/order';
$options = array('http' => array(
'method' => 'GET',
'content' => $data,
'header' =>
"Content-Type: application/x-www-form-urlencoded\r\n".
"Authorization: Bearer ".API_KEY
)
);
$context = stream_context_create($options);
$response1 = file_get_contents($url, false, $context);
print_r($response1) ;
Also I tried this bottom code, but it returns the error again!
$url = 'https://www.example.com/order?name=test&count=1000';
$options = array('http' => array(
'method' => 'GET',
'header' =>
"Content-Type: application/x-www-form-urlencoded\r\n".
"Authorization: Bearer ".API_KEY
)
);

Error in getting Active subscriber count from Sendy API

While trying to fetch the active subscriber count from Sendy API, I am getting following error
The POST request looks like this:
http://my-sandy-Installation/api/subscribers/active-subscriber-count.php
In header: Content-Type:application/x-www-form-urlencoded
In Body:
api_key= mykey
list_id= mylistid
Can anyone please help?
$boolean = 'true';
$postdata = http_build_query(
array(
'email' => $email,
'api_key'=>'your api_key',
'list' => 'Your List id',
'boolean' => 'true'
)
);
$opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata));
$context = stream_context_create($opts);
$result = file_get_contents($your_installation_url.'/subscribe', false, $context);

Automated login to Stackoverflow

I get quite annoyed by logging in to SO sometimes and i'd like to do it by just calling a PHP file. I tried doing so by sending a post request like this:
<?php
$url = 'https://stackoverflow.com/users/login?ssrc=head&returnurl=http%3a%2f%2fstackoverflow.com%2f';
$data = array('email' => 'mymail#gmail.com', 'password' => 'mypasswort');
$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);
?>
I also tried including more values like ssrc and fkey but i just shows the SO mainpage after submitting. It even stays at localhost (or wherever the script is running). If i enter a wrong password it marks it as incorrect - so it has to work at lest in some way (the data verification)...
You must send post data:
isSignup:false
isLogin:true
isPassword:false
isAddLogin:false
hasCaptcha:false
fkey:922b6dc5a5a375283c44e298246d7763
ssrc:head
email:asd
password:
submitbutton:Log in
oauthversion:
oauthserver:
openidusername:
openididentifier:
You should send first GET request and parse fkey from result page (this is CSRF protection).
fkey you should send with POST request with post data which I show you.
Simple example:
<?php
$url = 'https://stackoverflow.com/users/login?ssrc=head';
$data = array(
'email' => 'mail#email.z',
'password' => 'pass',
'isSignup' => false,
'isLogin' => true,
'isPassword' => false,
'isAddLogin' => false,
'hasCaptcha' => false,
'fkey' => '',
'ssrc' => 'head',
'submitbutton' => 'Log in',
'oauthversion' => '',
'oauthserver' => '',
'openidusername' => '',
'openididentifier' => ''
);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'GET',
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
preg_match('~name\=\"fkey\"\svalue=\"([\w]+)\"~', $result, $matches);
$data['fkey'] = $matches[1];
$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 I recommend for you use SO API and you willn't have any problems
If you will have problem, write

Using oauth2 in php for accessing twitter, moving it to Google App Engine

I found the code below on the net (here) and tried implementing it on Google App Engine.
It works perfectly when i ran it on localhost, but when i deployed it to App Enginge i get nothing in $encodedData.
$authContext = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Authorization: Basic " . base64_encode(($consumerKey).':'.($consumerSecret)) . "\r\n".
"Content-type: application/x-www-form-urlencoded;charset=UTF-8\r\n".
"Content-Length: 29\r\n".
"\r\n".
"grant_type=client_credentials",
),
));
$authResponse = file_get_contents("https://api.twitter.com/oauth2/token", false, $authContext);
$decodedAuth = json_decode($authResponse, true);
$bearerToken = $decodedAuth["access_token"];
$context = stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => "Authorization: Bearer " . $bearerToken . "\r\n".
"\r\n".
"grant_type=client_credentials",
),
));
$encodedData = file_get_contents('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=samkiesupdates&count='."100", false, $context);
Anyone who knows what i need to alter in the code to have it work on the Google App Engine server?
Maybe it says in the replies to the forum post i linked above, but sadly i don't speak german so i can't tell.
You should be putting the post body in the 'content' field of the http context options, and no need to specify the content length.
$authContext = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Authorization: Basic ".base64_encode(($consumerKey).':'.($consumerSecret))."\r\n".
"Content-type: application/x-www-form-urlencoded;charset=UTF-8\r\n",
'content' => 'grant_type=client_credentials',
),
));
Do that same for the other http request you make.
Just to clear this stuff up, and to make this whole question / answer more visible. Here is the working code example:
<?php
$consumerKey = <YOUR CONSUMER KEY>;
$consumerSecret = <YOUR CONSUMER SECRET>;
$authContext = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Authorization: Basic " . base64_encode(($consumerKey).':'.($consumerSecret)) . "\r\n".
"Content-type: application/x-www-form-urlencoded;charset=UTF-8\r\n",
'content' => "grant_type=client_credentials"
),
));
$authResponse = file_get_contents("https://api.twitter.com/oauth2/token", false, $authContext);
$decodedAuth = json_decode($authResponse, true);
$bearerToken = $decodedAuth["access_token"];
$context = stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => "Authorization: Bearer " . $bearerToken . "\r\n",
'content' => "grant_type=client_credentials"
),
));
$encodedData = file_get_contents('https://api.twitter.com/1.1/statuses/show.json?id=<YOUR TWEET ID>', false, $context);
$result = json_decode($encodedData);
Thanks for the help,
Nick

Categories