0

I'm using this little PHP function that I wrote to make calls to my master database:

$db_request = curl_init(DB_ROOT.'/action/register.php');
curl_setopt($db_request, CURLOPT_HEADER, 0);
curl_setopt($db_request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($db_request, CURLOPT_POSTFIELDS, http_build_query($variables));
$db_response = curl_exec($db_request);
curl_close ($db_request);
        
parse_str($db_response, $information);

On register.php I use the following to respond:

echo http_build_query(array(
    'test1'=>'value1',
    'test2'=>'value2',
    'test3'=>'value3'
));

My problem comes when trying to retrieve the first index of any given response. I can use var_dump($information) and will receive array(3) { ["test1"]=> string(6) "value1" ["test2"]=> string(6) "value2" ["test3"]=> string(6) "value3" }. However, when I try to echo $information['test1'], I receive this: Notice: Undefined index: test1 in....

Echoing anything other than the first index doesn't give me this problem.

Does anyone have any thoughts?

isshakes
  • 56
  • 7
  • If anyone finds themselves in a similar situation. Ensure that your PHP files are encoded correctly. One of mine was not encoded in "UTF-8" but "UTF-8-BOM" which was leaving a hanging  attached to my first key. Also don't make my rookie mistake and try to debug in browser, always look at the source. There's 2 hours of my life i won't get back. – isshakes Feb 23 '21 at 22:21

1 Answers1

1

parse_str return type is void

Update your code

parse_str($db_response, $information);
ExploitFate
  • 595
  • 2
  • 9
  • Sorry was testing something an accidentally left that `$information =` in place. Even with it removed the problem still remains. – isshakes Feb 23 '21 at 20:25
  • 1
    maybe some whitespace or other typo ? – ExploitFate Feb 23 '21 at 20:30
  • I don't think so, because if I modify the response array with a blank key at the beginning, then I can call on `test1` just fine. – isshakes Feb 23 '21 at 20:37
  • Could you `var_dump($db_response);` ? – ExploitFate Feb 23 '21 at 21:29
  • Yeah for sure, it responds with `string(41) "test1=value1&test2=value2&test3=value3"` – isshakes Feb 23 '21 at 21:41
  • 1
    I've been working on it. It might be something in my code that's cause the problem because when I run it purely as described above it's working, so I'm digging a little deeper. – isshakes Feb 23 '21 at 21:42
  • Well, I only suggest you to use json_decode and json_encode ) – ExploitFate Feb 23 '21 at 21:45
  • Yeah tried that too but when I try to json_decode, it comes up NULL. Even though I can echo the string just fine... everything is conspiring against me today. I appreciate you sticking with me, this long though. – isshakes Feb 23 '21 at 21:48
  • To use curl with JSON you should add `CURLOPT_HTTPHEADER` culr option (See example https://stackoverflow.com/a/43520261/3849743 ) and JSON header in register.php (https://stackoverflow.com/a/4064468/3849743) – ExploitFate Feb 23 '21 at 21:53
  • Your second suggestion was the right one. One of my included files was encoded incorrectly and I had "" appended to the first key. Which was causing both the parse_str & json_encode problems. Thank you for your help! – isshakes Feb 23 '21 at 22:17