Retrieving response from PHP OData SDK when using SaveResult method - php

I am using the PHP Odata SDK (http://odataphp.codeplex.com/). The documentation and community seems to be very limited.
I am attempting to save an object to the OData service and retrieve the response. When I execute the method, it successfully saves the object to the OData service, but just returns a successful or true result. It should return the ID of the object I just added (which it does).
$user = Users:CreatUser(array_of_data);
$proxy->AddUser($user);
$proxy->SaveResult();
How do I retrieve the actual response from the OData Service?
I have tried:
$result = $proxy->SaveResult();
But this does not work.

The location of the newly created entry should be returned in the Location header of the response, per the AtomPub specification.
If you were to surf to the location returned by the server in response to a successfully created entry, you should see an entry that looks exactly like the one in the HTTP POST response.

Related

How do you send back data when you get a webhook?

I have a quick question. I am new to webhooks and the service I am using requires a response. I am doing this in php and here are their instructions:
We require you to verify the ownership of the server you are making WebHook calls to by adding the following object parameters to your JSON response.
{ "details" : { "zippy_token":"xxxxxxxxxxxxxxxx" }}
After adding the JSON response code to your WebHook endpoint, come back here and click the green Add Webhook button. Zippykind will verify your WebHook by making a POST call to your WebHook URL with the handshake within the parameters, afterwhich will add the verified WebHook to your active list of WebHooks. You only need to do this once, after the WebHook has been verified, you can remove the zippy_token parameter from your JSON response.
I see how to get the data but how do I send the info needed (the token) back?
This will be done in PHP.
Thanks
If your site, or what you're developing is located at abc.com, you'd essentially need to create a php script for your webhook callback. abc.com/testwebhook.php.
Inside of the testwebhook.php, you'll output a JSON response with the data formatted as they expect to receive it { "details" : { "zippy_token":"xxxxxxxxxxxxxxxx" }}
In case the service you're interacting with (Zippy) is checking the header output of your response, you may need to set the header via PHP within your testwebhook.php script: header('Content-Type: application/json');
Example - testwebhook.php
`<?php
$output = '{ "details" : { "zippy_token":"xxxxxxxxxxxxxxxx" }}';
header('Content-Type: application/json');
echo $output;
exit;
?>
You'll need to ensure that your json is properly formatted and the endpoint doesn't expect more data returned.
Then the rest is explained ini your initial question. Create the webhook by entering the URL at the Zippy service that gave you those instructions and add the url to the script you setup abc.com/testwebhook.php.
That should be all you need to do.

Laravel forcing json response for api

I'm building an api at my company using laravel.
The problem I'm encountering is that if you send an api request without defining the correct header with the request you will get html back if there is a failure e.g. authorization failure or findOrFail() failure.
My thinking is that you never want to return html (even if the user has the wrong header).
I have a couple of solutions. In BeforeMiddleware.php I can manually insert a header into the request such as:
// Check if we are on an api route
$apiRoute = strncmp($uri, '/api/', 5) == 0;
// Insert the request header to force json response
if ($apiRoute){
$language = $request->header->add('Accept', 'application/json');
}
The 2nd solutions would be to throw an error if they don't have the correct header.
What would be the best way to enforce a json response, what is a good practice for handling api responses in laravel?
Once you detected that you are on your api path you are out of the woods and can indeed tackle your problem in the app\Exceptions\Handler.php file like suggested on How do you force a JSON response on every response in Laravel?.
For an open source project I created JSON exception objects by Microsoft format as output, but you can choose the jsonapi format (http://jsonapi.org/examples/#error-objects-basics) as you like:
https://github.com/StadGent/laravel_site_opening-hours/blob/develop/app/Exceptions/Handler.php
(note that on this implementation it is indeed depending from the headers, but you can use your path detection I think)

How can I create a RESTfull service to retrieve a json or xml file from a client

I have to write a RESTfull service in PHP which can send json data to the caller and retrieve json (or xml) data from the user. I know how to send json or xml data, but not how I can get data back from the user.
The simplest way is getting JSON data as POST or PUT body. To get PHP body:
$entityBody = file_get_contents('php://input');
and then decode json into a PHP object:
$requestBody = json_decode ( $entityBody);
If you are not sour the request body is XML or JSON then check Content-Type in the header.
A RESTful api usually doesnt expect a "response" from the client.
What you want to do is to create an endpoint for the client to POST the specific content.
/api/user/?json={...}
On the PHP side you can retrieve the data with $_REQUEST['json'] (which includes both POST and GET.
There is something called HATEOS that can be used for telling the client about links associated with the current resource (if you want to "chain" calls between the client and the service).

soap client api call

Kindly any one explain how to give request and when hit the soap api url i want to get the response using soap client call.
You can use an assign and an unset
$your_array[$your_newkey] = $you_array[$your_oldkey];
unset($arr[$your_oldkey]);

How to GET remote JSON or XML API data from within PHP and assign a return object as PHP variable?

What I'm doing:
I'm writing a custom program in PHP which pulls data via API from an online LMS service. Right now, I'm trying to implement the available single-sign-on functionality.
This part of the program needs to execute a GET request to the API when a button is clicked (via js or php POST or ?) and ultimately redirect the users browser to a URL which is supplied in the response from the API.
The API allows the choice of an XML or JSON response and I would prefer to use JSON but will make do with XML if needed.
From the API documentation on making requests:
All requests listed in this document should contain a content-type
(XML or JSON) in the request header and be prefixed with the following
base Uri: https://api.example.com/v1.svc
E.g. The Uri to GET a list of Users in XML format would be:
Content-Type: text/xml
GET https://api.example.com/v1.svc/users?apikey=MY-KEY&source=MY-APP
Below is what I'm trying to implement:
How to get the a user's LoginKey
Once you have the user id that you want to sign on you need to make a
GET request to /users/{user-id} which will return information about
the user. Included in this is a LoginKey which you can use to redirect
the user's browser to.
eg.
GET
https://api.example.com/v1.svc/users/USER-ID?apikey=YOUR_API_KEY&source=sampleapp
Response from API:
<User>
<Id>abc12345678</Id>
<UserName>rich_demo#example.com</UserName>
<FirstName>Rich</FirstName>
<LastName>Chetwynd</LastName>
.....
<LoginKey>https://demo.example.com/login.aspx?loginkey=xxxzzzyyy777222</LoginKey>
</User>
The <LoginKey> object data is the URL which I need to ultimately redirect the user's browser to.
I am new to working with APIs and have tried a ton of methods which I could not get to work before posting. If you know how to accomplish this I would be very grateful if you shared your knowledge.
Thanks.
From a HTML <form>, use a traditional post (not AJAX) to a PHP script that does this:
if(isset($_POST['userid']))
{
$userId = (int)$_POST['userid'];
$obj = simplexml_load_file('https://api.xxx.com/v1.svc/users/' . $userId . '?apikey=YOUR_API_KEY&source=sampleapp');
if($obj && isset($obj->LoginKey))
{
$loginKey = $obj->LoginKey;
header('Location: ' . $loginKey);
}
else
{
// failed to load the xml
}
}
If you want to do it with JSON you can use file_get_contents() to get the raw JSON from a URL, then use json_decode() to turn it into an object.
Also, if you want to do it via AJAX, you will have to echo the URL with PHP instead of trying to redirect, then have Javascript do the redirect with window.location.href = '...'

Categories