I have a web server where there are a few php scripts. These scripts are web service whereby some data is passed and they return values in json format.
For e.g. : http://www.example.com/test.php?name=abcd&id=100
So far passing values in url is the method and its ok if there is short content. However if I have to pass 1 page worth of content/text to it then there must be another way.
Currently my code is:
Dim web_client As Net.WebClient = New Net.WebClient
Dim Info As String
Dim response As IO.Stream = web_client.OpenRead("http://example.com/auth.php?userid=102&password=123")
Dim stream_reader As New IO.StreamReader(response)
Info = stream_reader.ReadToEnd()
' Close the stream reader and its underlying stream.
stream_reader.Close()
response.Close()
print (Info) ' info has output which is in json format
Is the above method ok and secure ? However if I wish to pass large amounts of data or the method is not secure then what should I do ?
Thank you.
Related
I am attempting to get my Access database written in VBA to communicate with a MySQL database on my clients website through a set of PHP web services I wrote. I have managed to get the Access db to retrieve data from the MySQL db but can't get it to post anything. I have narrowed the problem down to the fact that my HTTP request isn't sending the arguments I assign it.
Here are some questions and sites I have looked at already. These were not helpful, because the majority weren't dealing with PHP and were instead looking directly at websites, or were dealing with GET rather than POST.
Http Post not posting data
excel vba http request download data from yahoo finance
VBA HTTP GET request - cookies with colons
Pass Parameters in VBA HTTP Post Request
Perform hidden http-request from vba
How can I send an HTTP POST request to a server from Excel using VBA?
http://www.tushar-mehta.com/publish_train/xl_vba_cases/vba_web_pages_services/
https://stackoverflow.com/questions/29190759/vba-oauth2-authentication-http-get-request
VBA ServerXMLHTTP https request with self signed certificate
How can I send an HTTP POST request to a server from Excel using VBA?
Sending http requests with VBA from Word Sending http requests with VBA from Word
My code for VBA is:
Dim strJSONEncodedJob As String
strJSONEncodedJob = "[{""ExpenseID"":""" & astrExpenseIDs(intI) & "}]"
URL = "I removed the URL when posting"
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "Content-Type", "application/json"
objHTTP.send (strJSONEncodedJob)
strResponse = objHTTP.responseText
MsgBox strResponse
And my PHP code is:
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$json = file_get_contents('php://input');
$data = json_decode($json, true);
$stmt = $conn->prepare("DELETE FROM tblExpenses WHERE ExpenseID=?");
$txtExpenseID = $data['ExpenseID'];
$stmt->bind_param("i", $txtExpenseID);
$stmt->execute();
echo '{"result" : "success"}';
$stmt->close();
$conn->close();
I get the success statement as a msgbox in VBA, as expected, however the record is not deleted from the MySQL db.
Does anyone have a solution?
Thanks in advance.
Update
When I echo $json I get the JSON encoded string, which means the arguments ARE being passed. However, when I echo $data['ExpenseID'] I get a blank msgbox.
Your json-encoded object is an array containing a single object, so if you want to get at that object's properties you'd first have to index it from the array. Something like
$txtExpenseID = $data[0]['ExpenseID'];
(again, I'm not a PHP person so just guessing the syntax)
hey i am making an activation architecture in .net. I have written a script that posts the PHP variables, however, the PHP echos back a server response and i want to get that response back.
I have tried some code pieces but they don't work.
the sample php script is:
<?php
$name = $_POST['username'];
echo $name;
?>
i can't get the response, i.e. echo $name, i cant get it please help
my code is:
Dim requestStream As Stream = request.GetRequestStream()
requestStream.Write(postBuffer, 0, postBuffer.Length)
Dim stream As Stream = request.GetResponse().GetResponseStream()
Dim reader As New StreamReader(stream)
Dim response As String
Dim newStream As Stream = request.GetRequestStream()
While response = reader.ReadLine()
response += reader.ReadLine()
End While
Return response
requestStream.Close()
thanks
You may want to check my Answer to this post.
It's a bit more complicated than the answer you need, but it will solve your problem.
The Second Example is what you want. But don't just copy it, you need the class and the reference too.
Hope it helps.
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 have managed to send an xml via post using xmlhttprequest. I have also managed to read the whole xml syntax by an aspx page using
Dim reader As System.IO.StreamReader = New System.IO.StreamReader(Page.Request.InputStream)
Dim xmlData As String = ""
xmlData = reader.ReadToEnd()
I am now trying to read the xml from a php page. (I want to read the whole xml, headers and data)
using $_POST I am getting nothing
using file_get_contents("php://input") im getting the xml's data, no headers.
what am I doing wrong? How can I read the whole posted xml?
file_get_contents does the job i want.
althought mozilla displays the pure xml data file_get_contents has
OK, I am quite new to this Objective-C and JSON thing, so please bare with my questions. I am using Stig Json Framework and ASIHTTPRequest.
My overall objective is to send JSON data (containing login credential) to a PHP webservice and expecting a return once the PHP process the data. I need an authorize key from the return and also specific user data tied to the login credential.
Now, I can send the JSON data via POST all right, consume the data via PHP all right. But I just don't understand the logic of the return data, as I said earlier. I only know that ASIHTTPRequest do return the response string via anything echo'ed from the PHP, but is this the correct way to get the return data I want?
Or, do I encode it back to JSON? How?
I think another viable solution is to write a webservice that receive parameters via URL and return JSON data. For example, how do I write http://search.twitter.com/search.json?q=#<name>?
The data is returned as a JSON string and you need to convert it to Objective-C object using the "JSONValue" method before working with the returned data.
For example:
NSDictionary *results = [jsonString JSONValue];
Now you can work with the returned data as an NSDictionary.
You can json_encode data from PHP and echo that. Now you will receive JSON data in the ASIHTTPRequest delegate method, that is, a response string. Then use it as you like.
Remember to set the header type to json while you echo data from PHP.