0

I am trying to pass some JSON data through a php form I made:

<?php 
$json ='{"client_id": "mykey",
"client_secret": "mysecret",
"grant_type": "client_credentials"}';

$stuff = json_encode($json);


?>

<form action="https://api.yotpo.com/oauth/token" method="POST">

    <input type="hidden" name="post_data" <?php echo 'value="'.$stuff.'"'; ?> />
    <input type="submit">
</form>

However once submitted it appears that no data is passing to the next page.

Sackling
  • 1,780
  • 5
  • 37
  • 71
  • `json_encode()` expects an array, not a string. – Jay Blanchard May 16 '17 at 19:50
  • Do `print_r($_POST);` at the top of the next page and tell us what it contains. – Jay Blanchard May 16 '17 at 19:53
  • @JayBlanchard The next page is not my own. It is the api page I am trying to interact with. I can make it go to my own page though and report back just a sec. – Sackling May 16 '17 at 19:56
  • Array ( [post_data] => { ) is what I get – Sackling May 16 '17 at 19:57
  • Your string is already JSON, so there's no need to call `json_encode()` on it. Just `echo 'value="'.$json.'"';` – rickdenhaan May 16 '17 at 19:58
  • Since $json is already stringified, you don't need to call json_encode. But you do need to call htmlspecialchars() to safely escape it for use in html. – dbandstra May 16 '17 at 19:58
  • @dbandstra Ok that makes sense. I took away the encode. and added htmlspecialchars(). it now seems to work when using print_r($_POST) to test on my own page, however when I changed the form action back to the api website it still appears blank? – Sackling May 16 '17 at 20:01
  • Hmm, I guess that api requires the entire request to be in json format. I don't think you can do that with html forms directly. See this answer for example: http://stackoverflow.com/questions/6213509/send-json-post-using-php – dbandstra May 16 '17 at 20:09

0 Answers0