How to use Plivo call API - php

I am using plivo and have calls answered by welcome.php as my welcome url.
When a call is answered, I pass it to conf_handler.php and enter it into a conference:
$conf_attr = array(
'callbackUrl' => $host.'conf_handler.php',
'callbackMethod' => "POST",
);
$r->addConference($conf_name,$conf_attr);
echo($r->toXML());
How can I store both the callUUID and call_duration (once hang up) as a variables within conf_handler.php? Are they posted to the page with the callbackMethod? Or do I need to somehow use GET to look them up (how would I do this?) http://plivo.com/docs/api/call/#call_detail

you can do both ways.
1) From hangup_url (within your welcome.php). When a call is answered, Plivo sends a POST request with a set of parameters including the two. So you can parse the request from there. (Check out the "Request Parameters" section at http://plivo.com/docs/xml/request/)
2) From callbackUrl (within your conf_handler.php'). Once you set up theconf_handler.phpin thecallbackUrl`, Plivo will send a set of parameters as described at http://plivo.com/docs/xml/dial/#dialcallbackUrl
After that, you just need to parse the POST request and store as variables. (perhaps you could refer to http://www.tutorialspoint.com/php/php_get_post.htm)
Let me know if anything isn't clear. And I work at Plivo.

Related

Getting parameter from asynchronous HTTP POST call in PHP

I'm trying to integrate a virtual paying system from a bank into my website.
The bank system makes an asynchronous HTTP call to a URL that I define (a PHP document), and from there I try to get the order ID and mark the order as paid.
The problem I'm facing, is that with only the HTTP call I don't seem to be able to get this ID parameter, as the expected result (the couchdb documnt is marked as payed) is not there.
But, when I browse to my document and I paste after it the content of the HTTP call, then it works, like so:
http://example.com/crud.php?Ds_SignatureVersion=xxx&Ds_MerchantParameters=xxx&Ds_Signature=xxx
On my crud.php I do the following:
$test = $_GET['Ds_MerchantParameters'];
Why am I managing to get this parameter when I browse to the URL, but not when the bank system makes the HTTP POST call?
If the bank system issues a POST request to your site, the passed parameters are not in $_GET, but in $_POST. So change
$test = $_GET['Ds_MerchantParameters'];
to
$test = $_POST['Ds_MerchantParameters'];
If you want to be able to use both GET and POST requests for the page, you can also use $_REQUEST:
$test = $_REQUEST['Ds_MerchantParameters'];

How to create a endpoint in php using rest API

I hosted an empty page called api.php on a website
What I would like is to write a php or javascript script that will echo everything received from a callback function and display it without specifying the parameter of what the callback is sending ( as they may vary )
example :
var message = MessageResource.Create(
to: new PhoneNumber(toNumber),
from: new PhoneNumber(fromNumber),
body: msgBody,
provideFeedback: true,
statusCallback: new Uri("https://mywebsite/api.php"));// <--- the call back method will make a get or post request to the website I hosted
Now what should I write in the api.php ?
I tried to read the documentation on the rest api but the problem is ... I don't know what or how many parameter the statusCallback will send, so I want something like : no matter what http-request is coming , display the data in a human-friendly way.
Note : I'm aware of the previously answered question on Http Test server that accept GET/POST where sites like http://requestb.in/ and http://httpbin.org/, do the 90% job I'm trying to do , but I'm trying to do the same thing they are doing but using my own personal link so I can control the data the way I want
(the data being sent have 16k lines , and I want to chose when to erase them or do whatever)
-

Posting Data to REST API using JSON and PHP

I've been looking around at similar topics on REST APIs but I am still having some confusion in my project, mostly with the PHP side of things.
USPS provides a REST API with functions that can be called via URL like this: https://epfws.usps.gov/ws/resources/epf/login
To make any call successfully, I have been told that a JSON object must be created and passed as a "POST parameter" with the expected values.
This is the JSON object that needs to be passed in this case:
obj=
{
"login":"loginExample",
"pword":"passwordExample"
}
I have also been given a PHP class that is supposed to manage these calls. This is the login function:
public function login ()
{
// Set up the parameters for a login attempt
$jsonData = array(
'login' => $this->loginUser,
'pword' => $this->loginPass,
);
// Make a login request
$jsonResponse = $this->pullResource
('/epf/login', 'POST', $jsonData);
return $jsonResponse;
}
So I have a few questions regarding this:
The document they sent says
"To make the request calls, a JSON object will need to be created and passed as a POST form parameter obj={jsonObject} for security reasons using content-type “application/x-www-form-urlencoded”."
I know that the login function contains the correct input values that USPS' REST API is wanting, but I'm not sure how to pass them as "obj", or how to apply the "content-type".
I have a "constant" defined at the top of my PHP script that looks like this:
const EPF_BASE_URL = 'https://epfws.usps.gov/ws/resources';
And I noticed in the actual functions that this part of the link is left out and they simply reference '/epf/login' as you can see above. Since "$this" contains lots of different values I'm wondering how it supposedly finds EPF_BASE_URL as needed. Is it similar to how 'using' directives work in C#?
What is the easiest way to call this function and display the result? This is my biggest question. Would I use a separate PHP class with an HTML form? I understand the concept of what it should do but I'm completely lost setting up a development environment for it.
I've been trying all of this with MAMP but would love to know if I'm on the right track or not.
That really depends on their API. Hopefully you get a string back that can be decoded to a JSON object (http://au.php.net/manual/en/function.json-decode.php). Some API might give a simple string that says 'SUCCESS' or 'FAIL'. You've got the code, so take a look at what $this->pullResponse() gives you.
If you've been given a PHP class that is supposed to support the API (hopefully from USPS), then it should already take care of putting the data in the form content, and ensuring is it submitted with the appropriate content-type.
A PHP const is more like a C# static string. It is very likely that the library will use the constant to create the end URL (i.e. EPF_BASE_URL . $resource). If you needed to run against a sand box environment, you could change that constant without having to change all the other code.
That's a very big question, because it depends on how you are programming your application. Procedural, MVC, existing frameworks, etc.
At the very least, you would set the loginUser and loginPass on the instantiated object, and call the login method`. You could then inspect the results, assuming the result is a JSON object, or use your favourite debugging method to see the contents.
I'm having a guess as the USPS API class name.
$uspsApi = new UspsApi();
$uspsApi->loginUser = 'username';
$uspsApi->loginPass = 'password';
$result = $uspsApi->login();
echo print_r($result, true);

AJAX & PHP:GET parameters causing Flickr api key error

I'm trying to send a URL with aFLickr API key to fetch results for a given photo tag. The Ajax code should return the XML to my browser. However the URL structure with parameters seems to cause a problem in my setup:
**the HTML file:**
...
url="api.flickr.com/services/rest/?method=flickr.photos.search&api_key=75564008a468bf8a284dc94bbd176dd8&tags=paris"
request.open("GET","xmlget.php?url=" + url + nocache, true)
...
**the 'xmlget.php' file:**
...
echo file_get_contents($_GET['url']);
...
error: code="100" msg="Invalid API Key (Key has invalid format)">
the link works fine if tested in the adress bar so there must be a breakdown somewhere when the URL is processed.
i tried wrapping it into encodeURI but no luck :(
Note: related post
You need to use encodeURIComponent instead of encodeURI to actually get that string encoded.
May I make 2 suggestions?
just pass the search parameters to xmlget.php and do the rest there even if it means having to pass a service type if you are using that generically
I don't remember what all a Flickr api key gets you, but it's generally a bad thing to post anything called an "api key" in public. In addition to the question, that includes sticking it in javascript that an end user can access.

Receiving and handling a GET request in PHP

I'm trying to replace RSS polling with PubSubHubbub on my site. I'm able to use the subscriber library that google offers to send the subscription request. From the code it looks like it sends a post request via cURL with the RSS URL and a callback URL.
So this is where I need some direction:
In order to complete the subscription request my callback URL has to receive a GET request and then echo back a value from the GET request along with a 200 response. How do I get the parameters from the GET request? Is the echo done again via cURL? If so what option should include the 200 response?
This very simple script should be a start:
echo $_GET["request_name"];
this will output the GET parameter request_name and (implicitly) send a 200.
It's also a good idea to explicitly declare a content type before echoing, to prevent the default content type (usually "text/html") from kicking in:
header("Content-type: text/plain");
Note that when echoing external data, you may need to sanitize the output first - if the for example the output format is HTML, you would want to do something like echo htmlspecialchars($_GET["request_name"]); to prevent Cross-Site Scripting.
There was recently a thread on the php-dev mailing list about this. The reason you can't access 'hub.challenge' in the $_GET superglobal is due to register_globals. Basically PHP cleans up any argument names before creating the superglobals. Any dots will be converted to underscores. It's looking to be 'fixed' in PHP 6, but not before due to BC issues.
Here's the thread about it.

Categories