-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneedle.php
More file actions
53 lines (34 loc) · 1.09 KB
/
needle.php
File metadata and controls
53 lines (34 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
$token = array('token' => 'Zsh0UXjDre');
$json_token = json_encode($token);
function postData($url, $data) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
return $result;
}
$result = postData('http://challenge.code2040.org/api/haystack', $json_token);
//transfer JSON object to array
$obj = json_decode($result, TRUE);
//pass needle and haystack to string and array variables
$array = array();
for($i=0;$i<count($obj['result']['haystack']); $i++) {
$array[] = $obj['result']['haystack'][$i];
}
$string = $obj['result']['needle'];
// look for position of string in array
$position = array_search($string, $array);
//send error message if it's not found
if ($position === FALSE) {
echo "Could not find the string in the array.";
exit();
}
$info = array('token' => 'Zsh0UXjDre',
'needle' => $position);
$json_info = json_encode($info);
//Send info
$answer = postData('http://challenge.code2040.org/api/validateneedle', $json_info);
echo $answer;
?>