Fetching key details in php - php

This is the code from twilio api. Here 'callsid' is a query string.
$url = 'http://xxxx.com/phone/customer?to='.$number;
$call= $this->twilio->account->calls->get($this->request->query->get('CallSid'));
$call->update(array(
'Url' => $url,
'Method' => 'GET',
'StatusCallbackMethod' => 'GET',
'StatusCallback' => 'http://xxxx.com/phone/log/callback'
));
My question is that can we place an array key in the place of query string to fetch the key details? Like this:
$url = 'http://xxxx.com/phone/customer?to='.$number;
$call = $this->twilio->account->calls->get($this->request->query->get('url'));
$call->update(array(
'Url' => $url,
'Method' => 'GET',
'StatusCallbackMethod' => 'GET',
'StatusCallback' => 'http://xxxx.com/phone/log/callback'
));

Ricky from Twilio here.
If you want to filter by something other than CallSid with our PHP library you can use an iterator. For example this code will return calls that are currently in progress:
$filteredCalls = $client->account->calls->getIterator(
0, 50, array("Status" => "in-progress"));
foreach($filteredCalls as $call) {
print $call->price . '\n';
print $call->duration . '\n';
}
You can view a list of available filters in the docs.

Related

OpenSubtitles API 401 Unauthorized how to fix?

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.

Dropbox API - Error: expected list got dict

I am currently building a routine that needs to download files from one specific Dropbox folder , send them to another server and then move them to another folder on Dropbox.
I am using the /files/move_batch API endpoint for Dropbox to do so.
Here are the params sent to the API to move multiples files (well I'm only trying to move one file right now as it's still not working) :
$params = array(
'headers' => array(
'method' => 'POST',
'content-type' => 'application/json; charset=utf-8',
),
'body' => json_encode(array(
'entries' => array(
'from_path' => self::$files[0],
'to_path' => '/Applications/Archives/' . substr(self::$files[0], strrpos(self::$files[0], '/') + 1),
),
'autorename' => true,
)),
);
But I keep getting the same error message :
Error in call to API function "files/move_batch": request body: entries: expected list, got dict
I don't know what the API means by a list or how it should be formated.
The entries value should be a list of dict, one per file you want to move, each one containing both a from_path and a to_path. Your code is supplying the entries value to be a single dict though. (In PHP you can make both lists and dicts using the array keyword.)
It's easier to see and work with when you break it into pieces. Here's a working sample that does that.
<?php
$fileop1 = array(
'from_path' => "/test_39995261/a/1.txt",
'to_path' => "/test_39995261/b/1.txt"
);
$fileop2 = array(
'from_path' => "/test_39995261/a/2.txt",
'to_path' => "/test_39995261/b/2.txt"
);
$parameters = array(
'entries' => array($fileop1, $fileop2),
'autorename' => true,
);
$headers = array('Authorization: Bearer <ACCESS_TOKEN>',
'Content-Type: application/json');
$curlOptions = array(
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($parameters),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => true
);
$ch = curl_init('https://api.dropboxapi.com/2/files/move_batch');
curl_setopt_array($ch, $curlOptions);
$response = curl_exec($ch);
echo $response;
curl_close($ch);
?>
To move just one file using this batch endpoint, you would change that line to something like:
'entries' => array($fileop1),

Linkedin Invitation API Internal server error

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

Failed to send batch app notifications using facebook php SDK

I'm trying to send batch notification from my app to several app users after runing the below code I get an error in the response:
"{"error":{"message":"(#100) Must specify a non-empty template `param","type":"OAuthException","code":100}}"`
Although the template param is set...
Appreciate any help on what am i doing wrong..
Here is the code I use:
$batched_request = array();
foreach ($users as $idx => $user) {
$request = array(
'method' => 'POST',
'relative_url' => '/' . $user['id'].'/notifications',
'access_token' => $app_access_token,
'template' => $template,
'href' => $href
);
$batched_request[] = json_encode($request);
}
$params = array('batch' => '[' . implode(',',$batched_request) . ']' );
try {
$response = $facebook->api('/','POST',$params);
} catch(FacebookApiException $e) {
error_log($e);
}
if you post via batch api, please have in mind, that you should enclose the template & href parameter as a http query string within the "body"-key.
for example:
$apiCalls[] = array(
"method" => "POST",
"relative_url" => $user['id'] . "/notifications",
"body" => http_build_query(array("href" => $href, "template" => $template, "ref" => "ref_key")),
"access_token" => $app_access_token
);

Batch Requests and attached files with PHP SDK

I'm trying to make a batch request to post an unique photo on differents page.
For that, I wish to use Batch Post Requests to optimize the proccess.
My Code :
$facebook = new Facebook(array('appId' => myappId, secret => mysecret, 'cookie' => true, 'fileUpload' => true, 'domain' => $_SERVER['SERVER_NAME']));
$request[0] = array(
'relative_url' => 'facebookPageId1/photos'
'method' => 'post'
'body' => 'access_token=page_access_token_1&message=my_message&attached_files=' . basename($picture));
$request[1] = array(
'relative_url' =>'facebookPageId2/photos'
'method' => 'post'
'body' => 'access_token=page_access_token_2&message=my_message&attached_files=' . basename($picture));
$file[basename($picture)] = '#' . realpath($picture);
$batch = json_encode(array_values(requests));
$params = array('batch' => $batch);
$params = array_merge($params, $file);
$facebook->api('/', 'POST', $params)
Now when I am running this code I got the following output for my two requests :
'{"error":{"message":"(#324) Requires upload file","type":"OAuthException","code":324}}'
So what's the problem ?
I set fileUpload at true on my Facebook Object and I tried to post a photo on the url "pageId/photos" with a classic request and it's worked perfectly. But witch a batch request, I have always the same error.
Thanks for your help.
EDIT : Ok I get my mistake, my requests was wrong :
$request[0] = array(
'relative_url' => 'facebookPageId1/photos',
'method' => 'post',
'body' => 'access_token=page_access_token_1&message=my_message',
'attached_files' => basename($picture)
);
$request[1] = array(
'relative_url' =>'facebookPageId2/photos',
'method' => 'post',
'body' => 'access_token=page_access_token_2&message=my_message',
'attached_files' => basename($picture)
);
But now I got the following error :
{"error":{"message":"File picturename.jpg has not been attached","type":"GraphBatchException"}}
This is what i do:
$files['access_token']=$access_token;
$request[0] = array(
'relative_url' => 'facebookPageId1/photos',
'method' => 'POST',
'body' => 'message=my_message',
'attached_files' => 'file_0'
);
$files['file_0']= basename($picture);
$batchresult = $facebook->api("/?batch=".urlencode(json_encode($request)), 'POST', $files);
Facebook Batch ask you to put the files in diferent arrays, also you can put the access token in there, and you need to put it once.

Categories