Skip to content

Instantly share code, notes, and snippets.

@chrisyue
Created January 15, 2014 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisyue/8434135 to your computer and use it in GitHub Desktop.
Save chrisyue/8434135 to your computer and use it in GitHub Desktop.
使用file_get_content发送POST请求
function tweet($message, $username, $password)
{
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => sprintf("Authorization: Basic %s\r\n", base64_encode($username.':'.$password)).
"Content-type: application/x-www-form-urlencoded\r\n",
'content' => http_build_query(array('status' => $message)),
'timeout' => 5,
),
));
$ret = file_get_contents('http://twitter.com/statuses/update.xml', false, $context);
return false !== $ret;
}
// 其中content 就是 request body, 可以使用json格式如果服务器支持('content' => json_encode(array('status' => $message))),比如elasticsearch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment