【发布时间】:2014-06-27 09:23:34
【问题描述】:
没有你的帮助,我什么都做不了。
我的任务是通过 LinkedIn API 在公司页面上将帖子分享到 LinkedIn。 GET 请求对我来说工作正常,但是当我尝试创建 POST 请求时,我收到以下错误消息:
file_get_contents(https://api.linkedin.com/v1/companies/3723615/shares?visbility%5Bcode%5D=anyone&comment=test+comment): failed to open stream: HTTP request failed! HTTP/1.0 400 request#no_content_length
所以.. 这是我的代码的一部分(我从官方文档中获得此代码):
public function index() {
....
// This GET request works fine
$company = $this->companyInfo('GET', '/v1/companies/universal-name=ovdwebdev');
print_r($company);
// This one give me error
$post = $this->companyPost('POST', '/v1/companies/3723615/shares');
exit;
}
private function companyInfo($method, $resource, $body = '') {
$params = array('oauth2_access_token' => $_SESSION['access_token'],
'format' => 'json',
);
// Need to use HTTPS
$url = 'https://api.linkedin.com' . $resource . '?' . http_build_query($params);
// Tell streams to make a (GET, POST, PUT, or DELETE) request
$context = stream_context_create(
array('http' =>
array('method' => $method,
)
)
);
// Hocus Pocus
$response = file_get_contents($url, false, $context);
// Native PHP object, please
return json_decode($response);
}
private function getAuthorizationCode($api_key, $scope, $redirect_uri) {
$params = array('response_type' => 'code',
'client_id' => $api_key,
'scope' => $scope,
'state' => uniqid('', true), // unique long string
'redirect_uri' => $redirect_uri,
);
// Authentication request
$url = 'https://www.linkedin.com/uas/oauth2/authorization?' . http_build_query($params);
// Needed to identify request when it returns to us
$_SESSION['state'] = $params['state'];
// Redirect user to authenticate
header("Location: $url");
exit;
}
【问题讨论】: