Is there any way to send simple string
from websiteA.com -to-> websiteB.com with php?
Where websiteB.com "listens"(as not knowing the existence of websiteA.com) any incoming simple strings sent and receives it?
Setup a script on websiteB that handles a POST request (e.g. a string).
Use cURL or sockets on websiteA to send POST data (e.g. a string) to websiteB
Very very simple vay is to use "rest api" - eg. create PHP file, which will recieve string and process it , and return XML / JSON ... or other formatted ( or unformatted ) answer. Client site ( eg. A ) just need to fopen ( or send request using cURL ) this php file with approtiate string
More complex way is to use SOAP - it have standart object in PHP , but it is reccomed to write wsdl file, which is a bit complex. see google for details
Related
I'm a bit lost between definitions, generated code and many things that are a bit of a black box, even after debugging.
But let's start at the beginning. I have an API written with node using NestJS as framework. NestJS automatically creates the swagger/openapi json file. Using swagger-codegen I create a PHP class to access the API from another server. Works like a charm for simple API request.
Now the problem are API request returning a bigger response, i.e. >1000 rows from one or more DBs. Do make the client not wait a long time and create a big JSON response on the server I've switched to NDJSON, which splits the reponse in chunks of smaller JSON parts, each on it's own line. This also works when I create my request by hand with curl or a HTTP wrapper in PHP using fopen and fread. The response type is application/x-ndjson.
But the code generated by swagger-codegen is always waiting until the whole response has been received. It's even worse, because it fails to decode NDJSON with json_decode() and just returns null. Underneath Guzzle is used, which uses PSR7 streams for the response.
Now I could just skip the autogenerated code for the NDJSON endpoints. But I'd prefer not to add special handling and lose all the useful generated checks.
So is it somehow possible to make swagger-codegen give access to the stream of the response? Am I missing a parameter to codegen or something in the swagger JSON? It does have a produce with application/x-ndjson.
Do answer my own question it is possible, but not easily - meaning there is no option or parameter.
First create a class that extends the autogenerated API class. There you have access to all of the protected methods. We assume the endpoint or api method is called testMethodGet. The only thing that can be reused is the request method, i.e. testMethodGetRequest, but it does all of the client side validation and transforming of input data, so that's already a big win. Do also get some of the boilerplate result validation you can copy the "http info method", i.e. testMethodGetWithHttpInfo (use async version if you wish). Remove the if/else block after $responseBody = $response->getBody(); and in the return replace the ObjectSearializer line with just $responseBody.
What you are now getting back is the body as PSR7 stream, but there is one last catch. By default the whole response is drained into a temporary file. To really stream the result body from the server in the created method add an option to $options: $options['stream'] = true; Now the method returns as soon as the body is started. No need to wait for the whole body content.
One more thing. As the result is a PSR7 stream you can use $stream->detach() to get the PHP stream resource if you prefer to use the normal file and stream methods.
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).
I am following this tutorial to make an events calendar-it utilizes backbone and the fullcalendar jquery plugin.
Backbone is responsible for sending to the server(via ajax) event details(start date,end date,title).Here is an image of what is sent to the server.
It is taken by the network panel(headers tab) of Chrome Dev Tools. I would expect that with the following line of code I would access the title of the event:
$title=$conn->real_escape_string($_POST['title']);
But I cannot, I do not understand why this happens. backbone sends JSON to server via the POST method. What am I missing here?
PHP has a problem with parsing json data, because it expects the posted data to be in a Querystring format (key=value&key1=value1).try using this:
$content = file_get_contents("php://input");
You are sending a JSON dictionary in the request body. Use http_get_request_body in PHP to obtain the full JSON string, then json_decode it.
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 = '...'
I am building an API using Codeigniter and Phils RESTserver and I am trying to send an array to the API using a POST request.
When I POST this:
query=("email#example.com","anna#nicole.com")
I can access it like this:
$this->post('query');
This produces:
("email#example.com","anna#nicole.com")
How can I loop through these email addresses in PHP?
Do anyone got another idea?
Thankful for all input!
If you are controlling the POST request, then you should POST an array to PHP, not a delimited list, which will automatically create an array that is available for use when the server receives the request. This is what J0HN means when he says use square brackets instead of parenthesis.
However, it depends on how you're sending the request to the server to determine the proper way to send an array to PHP. You might need:
query=["one#example.com", "two#example.com"]
query[]=["one#example.com", "two#example.com"]
Or other variants...
Since you don't elaborate on how you're making the request to the server, the proper way to POST an array cannot be determined.
On the other hand, if you can't POST an array for some reason, you can always send a JSON-encoded string and easily decode it:
// Assuming $this->post('query') = json_encode( array( "one#example.com", "two#example.com"));
$emails = json_decode( $this->post('query')); // Array of emails