How can I use a php variable in a JSON request body?

时间:2017-08-30 20:26:12

标签: php json

I have a request body that looks like this:

scale

I want to assign the username php variable to the "username" json parameter, but I keep causing 400 bad requests when I try:

import scipy.stats as scs
import numpy as np
import pandas as pd
np.random.seed(123)
r = pd.Series(np.random.beta(a=0.5, b=0.5, size=1000),
              index=pd.date_range('2013', periods=1000))
ewm = r.ewm(span=60)
loc = ewm.mean().iloc[-1]
scale = ewm.std().iloc[-1]
print(scs.norm.ppf(q=0.05, loc=loc, scale=scale))
# -0.196734019969

2 个答案:

答案 0 :(得分:6)

Try:

updateSize(val, side) {
    const maxBombNumber = this.calculateMaxBombNumber();
    const bombNumber = maxBombNumber < this.state.bombNumber ? maxBombNumber : this.state.bombNumber;
    this.setState({ 
        [side]: val,
        maxBombNumber,
        bombNumber
    });
}

Work with array and use json_encode() to get the JSON

Why ?

PHP dont work naturally with JSON like Javascript, and concatenate and construct a JSON with a raw string is a bad practice and a headache.

答案 1 :(得分:5)

When you encode the username it looks like this:

  display: flex;
  align-items: center;

So, when you concatenate you're adding in the extra quotes, causing your JSON to be malformed. The result is:

.song {
  /*DivButtonTitle*/
  margin-bottom: 3px;
  color: #551A8B;
  font-size: 35px;
  font-family: 'flat';
  height: 80px;
  border-top: solid 1px #551A8B;
  border-bottom: solid 1px #551A8B;
  width: 100%;
  line-height: 80px;
  padding: 0px;
  display: flex;
  align-items: center;
}

What you should do is just concatenate directly:

<div class="song">
  <button class="start" id="1" name="Gentleman-GUE/01._T_Apposto.mp3" onclick="togglePlay(this.id, this.name)">start</button>
  <button class="restart" onclick="toggleRestart()">restart</button>
  <a>T'Apposto</a>
</div>

Concatenating directly requires that you modify the JSON in "something@something.com" to add in the necessary backslashes for the value as well as the addition quotes to insure the concatenation happens correctly. That will insure your JSON is correct.