Check HTTP request for string part
-
Hi,
I want to use a HTTP request to check for a valid license.
HTTP request works, when I switch on the response message I receive the output from the website correctly.
Now I want to check, if some parts of the response are adequate.
This is the complete response:
Array ( [api_version] => 1.020 [current_time] => 2017-09-08 00:12:47 [result] => success [data] => Array ( [is_license_valid] => Y [is_license_key_found] => Y [purchase_id] => ALE7KD7W [license_key] => RB73P-VVRS7-W3KEH-S4WSC-8UZC3-9JDQZ-D75EQ-VZMC7 [billing_tatus] => completed [billing_tatus_msg] => Vollständigt bezahlt [last_payment_at] => 2017-09-07 [last_payment_at_msg] => 07.09.2017 [next_payment_at] => [next_payment_at_msg] => [last_transaction_type] => payment [last_transaction_type_msg] => Zahlung [are_returned_data_public] => N ) )I only want to check for
[is_license_valid] => YIf this is missing or "N", I want to stop the EA, if it is "Y" it should run as expected.
How can I set up a condition to check this part of a string?
Best regards,
Sebastian -
This is some PHP type of response, but MQL is very stupid, it doesn't have those complex web abilities. The response that is expected is either plain text or values separated by some delimiter

-
This is the plain text output

Output from the website is also possible as JSON, PHP or XML.
maybe XML is good for searching for a string, but how can I access the HTTP response in a condition (not the first condition block, but the second with code input)?
-
MQL4 sees that answer as plain text, there is no XML, JSON or another parser. Really, the only format that I made it available to read is delimiter-separated format, something like this - value1,value2,value3 - if the Delimiter is set to comma in the HTTP block. Then, in Condition you can access value2 for example, if you set Key to be 1 (because Key is the position in the delimiter-separated string if the first position is 0).
-
ok, can you provide me a code sample to access that value? what is the name of the response variable?
-
What value? I told you how it works - the request is sent with a MQL4 function, then the response is turned into an array, and the data from that array is read somewhere in the Condition block. The array's name is FXD_HTTP_RESPONSE and it has at least 1 value (for key 0) or multiple values if delimiter was used. But don't use that variable name for nothing, this is some variable that I made for this situation and I may change it at any time.
Again, JSON, XML or any other ways to represent an associative array doesn't work here. For MQL4/MQL5 your response means nothing - it's just a string that contains many letters and some [ and ] symbols, nothing special. And in fact, in MQL4/MQL5 we don't even have associative arrays. Arrays in MQL4/MQL5 can only have numeric keys, or I don't know how they can have string keys.
-
Let me make this more clear.
I created this process:

Now I want to check the HTTP response, and the condition should be true if the response CONTAINS my string. So I think I need a "contains" in the Equals list, not "==", correct? How can I implement this?
-
Anybody an idea how to solve this?
-
@sebabahn I think that could help you. Try to find the string and get the result... https://www.mql5.com/en/docs/strings/stringfind
search for "[is_license_valid] =>". Something like this:
StringFind(
YOURstring, // string in which search is made
"[is_license_valid] =>", // what you want to find
0 // from what position search starts
);... and then you can use a StringSubstr to extract the result. Not so elegant but, considering the mql language limitations...
StringSubstr(
YOURstring, // string
start_pos, // position to start with from your code above (StringFind)
length=-1 // length of extracted string considering "[is_license_valid] =>" length
);