Having some problem on the chatbot that I'm currently developing it replies to messages nonstop.
I'm pretty new to this, the code below I just copied the code below to a youtube video the code is working but I'm not able to get why it was sending too many messages
<?php
file_put_contents("fb.txt", file_get_contents("php://input"));
$fb = file_get_contents('fb.txt');
$fb = json_decode($fb);
$sid = $fb->entry[0]->messaging[0]->sender->id;
if($sid == "105140841657941"){
exit();
}
$data = array(
'messaging_type' => 'RESPONSE',
'recipient' => array('id' => "$sid"),
'messsage' => 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);
$token = "SAMPLE TOKEN";
file_get_contents("https://graph.facebook.com/v10.0/me/messages?access_token=$token", false, $context);
file_put_contents("fb.txt", "");
?>
Related
I'm trying to fetch subtitles from OpenSubtitles (http://trac.opensubtitles.org/projects/opensubtitles/wiki/XMLRPC) like this:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
//Opensubtitles listing
function data($request){
$context = stream_context_create(array('http' => array(
'method' => "POST",
'header' => "Content-Type: text/xml\r\nUser-Agent: PHPRPC/1.0\r\n",
'content' => $request
)));
$server = 'http://api.opensubtitles.org/xml-rpc'; // api url
$file = file_get_contents($server, false, $context);
$response = xmlrpc_decode($file);
return $response;
}
//Get token
$request = xmlrpc_encode_request("LogIn", array('', '', 'eng', 'TemporaryUserAgent'));
$token = data($request)['token'];
//Get listing
$request = xmlrpc_encode_request("SearchSubtitles", array(
'imdb' => '0462499',
'sublanguageid' => 'eng',
'season' => '',
'episode' => '',
'token' => $token
));
$response = data($request);
var_dump($response);
?>
However I keep getting 401 Unauthorized. Does anyone know how to fix this problem? I know it's not a problem with the API because I am able to retrieve the token just fine.
Try using your username/password instead empty string.
And change UserAgent in TemporaryUserAgent in Header as written in
http://trac.opensubtitles.org/projects/opensubtitles/wiki/DevReadFirst
The second request should be in the following format:-
$request = xmlrpc_encode_request("SearchSubtitles", array($token, array(array('sublanguageid' => 'eng', 'imdbid' => 'your_imdbid'))));
Hope this helps.
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);
I made custom register page on wordpress using google recaptcha. It worked fine, without any errors but when i copied a code to new project it isnt working. No clue why. No secret and sitekey errors.
if(!empty($_POST['g-recaptcha-response']))
{
$gurl = 'https://www.google.com/recaptcha/api/siteverify';
$gdata = array(
'secret' => 'secret',
'response' => $_POST['g-recaptcha-response']
);
// use key 'http' even if you send the request to https://...
$goptions = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($gdata),
),
);
$gcontext = stream_context_create($goptions);
$gresult = file_get_contents($gurl, false, $gcontext);
var_dump($_POST['g-recaptcha-response']);
$gresult=json_decode($gresult);
if(!$gresult->success)
$register_errors[]='Error reCAPTCHA.';
}
UPDATE:
output of var_dump($_POST['g-recaptcha-response']) is some string propably hashed. $gresult = file_get_contents($gurl, false, $gcontext); is giving FALSE on var_dump($gresult);
Where did I make a mistake? My PHP-script:
<?php
// Set username and password
$lgname = "someUsername";
$lgpassword = "somePassword";
// First login to receive 1) token, 2) sessionid and 3) cookieprefix
$parameters = array('action' => 'login', 'lgname' => "$lgname", 'lgpassword' => "$lgpassword", 'format' => 'json');
options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($parameters)
),
);
$context = stream_context_create($options);
$result = file_get_contents("http://en.wikipedia.org/w/api.php", false, $context);
// Echo out the answer from MediaWiki-API
echo "$result";
// Put the needed parts of the answer into variables and echo them out
$array = json_decode($result,true);
$token = $array["login"]["token"];
$sessionid = $array["login"]["sessionid"];
$cookieprefix = $array["login"]["cookieprefix"];
echo "</BR>token: $token, sessionid: $sessionid, cookieprefix: $cookieprefix</BR>";
// Second login to 1) post token and 2) send sessionID within the header
$parameters = array('action' => 'login', 'lgname' => "$lgname", 'lgpassword' => "$lgpassword", 'lgtoken' => "$token", 'format' => 'json');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
"Cookie: " . $cookieprefix . "_session = $sessionid\r\n",
'method' => 'POST',
'content' => http_build_query($parameters)
),
);
$context = stream_context_create($options);
$result = file_get_contents("http://en.wikipedia.org/w/api.php", false, $context);
// Echo out result
echo "$result";
?>
What I get as an answer to my second POST-request is (exactly the same as to my first POST-request) that I need a token (even though I posted the token and even the sessionID in my second POST-request):
{"login": {
"result":"NeedToken",
"token":"82b3f2e1f1aa702ca6ceae473bb16bde",
"cookieprefix":"dewiki",
"sessionid":"531143bd7425722bf1be88e520dea6d5"}
}
The mistake is in using file_get_contents() in the first place. Use a PHP library for the MediaWiki web API, instead.
If you really want to do things yourself, ask a token from meta=tokens.
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