|
Post by sktbrd2 on Aug 26, 2020 16:01:24 GMT -5
Hello LB folks, I am developing a desktop application that part of its responsibility is to POST information up to a SQL database. While I have that functionality working, its the response of the php script that I cannot seem to read in LB. I have full access to both sides, only I had not really dealt with php before, so I hope the problem isn't in the script I put together.
The server information has been changed (so running this will not generate a proper response), but Ive included the get_response.php to have a look at how its
open "WININET.DLL" for dll as #net calldll #net, "InternetOpenA", "Program Name" as ptr, 0 as long, _ 0 as long, 0 as long, 0 as long, hnet as ulong
calldll #net, "InternetConnectA", hnet as ulong, "enje2wjp5wmkqfu.m.pipedream.net" as ptr, _ 'A free online resource to check API calls 443 as long, 0 as long, 0 as long, 3 as long, 0 as long, 1 as long, hcnt as ulong
calldll #net, "HttpOpenRequestA", hcnt as ulong, "POST" as ptr, "get_response.php" as ptr, _ 0 as long, 0 as long, 0 as long, 8388608 as long, 1 as long, hrqst as ulong header$ = "Content-Type: application/x-www-form-urlencoded" headerlen = len(header$)
content$ = "your_name=John&your_email=JDoe@yahoo.com&your_phone=8675309&comments=123" contentLen = len(content$)
calldll #net, "HttpSendRequestA", hrqst as ulong, header$ as ptr, headerlen as long, _ content$ as ptr, contentLen as long, res as ulong
print "POST: ";res 'Boolean so 1 is successful
''''''''''''''''''''''''''''''''''''''' 'Now read the echo from the php script! '''''''''''''''''''''''''''''''''''''''
'Variable for message buffer$ = space$(500)
'Pointer for the initial buffer size and then how much was actually written struct bufferLength, bLength as long struct.bufferLength.bLength = len(buffer$)
'Pointer for header indexes, once returned, this is the next header struct index, bindex as long struct.index.bindex = 0
'HTTP_QUERY_STATUS_TEXT is defined as 20 in wininet.h calldll #net, "HttpQueryInfoA", hrqst as ulong, _ 20 as long, buffer$ as ptr, bufferLength as struct, index as struct, res as long
print "Reply: ";res;", ";struct.bufferLength.bLength;", ";struct.index.bindex;", ";buffer$
'Alt attempt 'calldll #net, "InternetGetLastResponseInfoA", bufferLength as struct, buffer$ as ptr, index as struct, res as long 'print "Reply: ";res;" ";buffer$
calldll #net, "InternetCloseHandle", hrqst as ulong, res as long calldll #net, "InternetCloseHandle", hcnt as ulong, res as long calldll #net, "InternetCloseHandle", hnet as ulong, res as long close #net
Here is the the PHP script on the server and I am hoping to read the echo. More so if there was a DB error and for more advanced php scripting outputs once I get the reply parsed.
<?php require_once("config.php"); //Connects to the SQL DB
if((isset($_POST['your_name'])&& $_POST['your_name'] !='') && (isset($_POST['your_email'])&& $_POST['your_email'] !='')) { require_once("contact_mail.php");
$yourName = $conn->real_escape_string($_POST['your_name']); $yourEmail = $conn->real_escape_string($_POST['your_email']); $yourPhone = $conn->real_escape_string($_POST['your_phone']); $comments = $conn->real_escape_string($_POST['comments']); $sql="INSERT INTO Users (name, email, phone, comments) VALUES ('".$yourName."','".$yourEmail."', '".$yourPhone."', '".$comments."')"; if(!$result = $conn->query($sql)){ die('There was an error running the query [' . $conn->error . ']'); } else { echo "Submission Success"; http_response_code(100); //Attempt to read status code too? } } else { echo "Please fill Name and Email"; } ?>
|
|
|
Post by Chris Iverson on Aug 26, 2020 17:56:29 GMT -5
What info exactly are you trying to get?
Is the script outputting something that you want to see? Are you trying to see "Submission success"?
Because what you're retrieving with your code as written is the STATUS TEXT. Not the content of the page, and not even the status CODE. JUST the status text.
This is significant because as far as I can tell, the http_response_code() function in PHP does NOT set the status text. I don't know if there's a function that does.
Because there's no status text set, I suspect you won't get anything back from the HTTP_QUERY_STATUS_TEXT query.
If you want the status CODE, then you'll want to use HTTP_QUERY_STATUS_CODE(19).
If you want the content of the page(in the case of a successful test, "Submission Success"), then you'll need to use InternetReadFile().
|
|
|
Post by Chris Iverson on Aug 26, 2020 18:46:30 GMT -5
You can ignore most of my last post.
Couple things I found out from actually being able to run the code.
Most important: you're accessing the structs wrong, and thus not actually providing the proper data.
When accessing a struct member, the syntax is structName.structMember.struct, not struct.structName.structMember.
Switch around the mistakes in your code, and things start working.
Additionally, I was wrong about not being able to get a status text string. I think the system generates those automatically; my server certainly isn't sending them, but they show up if you ask for them on the client.
(I will note that HTTP Status 100 Continue has a special meaning, and if you send it when the client isn't going to be sending more data, it looks like WINHTTP will silently convert it to 200 OK. If you're expecting the 100 Continue response, and haven't sent anything else, this may trip you up.)
Here's your code, modified to work against a quick script I have running on my server:
open "WININET.DLL" for dll as #net calldll #net, "InternetOpenA", "Program Name" as ptr, 0 as long, _ 0 as long, 0 as long, 0 as long, hnet as ulong
calldll #net, "InternetConnectA", hnet as ulong, "chrisiverson.net" as ptr, _ 'A free online resource to check API calls 443 as long, 0 as long, 0 as long, 3 as long, 0 as long, 1 as long, hcnt as ulong
calldll #net, "HttpOpenRequestA", hcnt as ulong, "POST" as ptr, "test.php" as ptr, _ 0 as long, 0 as long, 0 as long, 8388608 as long, 1 as long, hrqst as ulong
header$ = "Content-Type: application/x-www-form-urlencoded" headerlen = len(header$)
content$ = "your_name=John&your_email=JDoe@yahoo.com&your_phone=8675309&comments=123" contentLen = len(content$)
calldll #net, "HttpSendRequestA", hrqst as ulong, header$ as ptr, headerlen as long, _ content$ as ptr, contentLen as long, res as ulong
print "POST: ";res 'Boolean so 1 is successful
''''''''''''''''''''''''''''''''''''''' 'Now read the echo from the php script! '''''''''''''''''''''''''''''''''''''''
'Variable for message buffer$ = space$(500)
'Pointer for the initial buffer size and then how much was actually written struct bufferLength, bLength as long bufferLength.bLength.struct = len(buffer$)
'Pointer for header indexes, once returned, this is the next header struct index, bindex as long index.bindex.struct = 0
'HTTP_QUERY_STATUS_TEXT is defined as 20 in wininet.h HTTP.QUERY.STATUS.CODE = 19
calldll #net, "HttpQueryInfoA", hrqst as ulong, _ 20 as long, buffer$ as ptr, bufferLength as struct, index as struct, res as long
print "Reply: ";res;", ";bufferLength.bLength.struct;", ";index.bindex.struct;", ";buffer$
HTTP.QUERY.CONTENT.LENGTH = 5
buffer$ = space$(500) bufferLength.bLength.struct = 500
calldll #net, "HttpQueryInfoA", hrqst as ulong, _ 5 as long, buffer$ as ptr, bufferLength as struct,_ index as struct, res as long
print "res ";res print "Content Length: ";buffer$
bufLen = val(buffer$) buf$ = space$(500) struct a, bytesRead as ulong
callDLL #net, "InternetReadFile", hrqst as ulong,_ buf$ as ptr, bufLen as long, a as struct,_ ret as long
print "IRFA ret - ";ret
print buf$
'Alt attempt 'calldll #net, "InternetGetLastResponseInfoA", bufferLength as struct, buffer$ as ptr, index as struct, res as long 'print "Reply: ";res;" ";buffer$
calldll #net, "InternetCloseHandle", hrqst as ulong, res as long calldll #net, "InternetCloseHandle", hcnt as ulong, res as long calldll #net, "InternetCloseHandle", hnet as ulong, res as long close #net
|
|
|
Post by sktbrd2 on Aug 27, 2020 7:23:57 GMT -5
Chris, thankyou for explaining that a bit better and finding that simple mistake with the structs! Im not really after the status code (was just an attempt to read) or the status text, I incorrectly assumed the output of the php could be read with the status text query, but that makes sense why it wouldn't. This is merely a baseline test to read a reply once posting something.
Would you be kind enough to show the test.php script you put in place to echo the data?
|
|
|
Post by Chris Iverson on Aug 27, 2020 11:26:28 GMT -5
Sure, this is the test code I had.
<?php
$postData = print_r($_POST, true); header("Content-Length: " . (strlen($postData) + 1)); http_response_code(200);
echo $postData;
?>
Setting the response code to 200 is redundant, but I was using that line to check various response codes.
In addition, I manually generate a Content-Length header as well. The only reason I did that was to test getting the content length from HttpQueryInfo(). PHP doesn't automatically generate that header because it's dynamic content; you often won't know the size of the page in advance unless you pre-render it.
|
|