Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php在url中编辑json格式的数据?_Php_Json - Fatal编程技术网

如何使用php在url中编辑json格式的数据?

如何使用php在url中编辑json格式的数据?,php,json,Php,Json,这是我的urlhttp://localhost:4000/users json数据如下所示: "users": [ { "id": "1", "name": "Admin", "email": "admin@gmail.com", "username": "admin", "passwor

这是我的url
http://localhost:4000/users

json数据如下所示:

"users": [
{
  "id": "1",
  "name": "Admin",
  "email": "admin@gmail.com",
  "username": "admin",
  "password": "e19d5cd5af0378da05f63f891c7467af",
  "wallet_address": "4p7tg5cb2yulvmz3sjfeanoi0r8wkdx4h69q",
  "balance": "1000"
},
]
我想改变平衡。我该怎么做?我尝试了以下代码:

$url = "http://localhost:4000/users";
$data = file_get_contents($url);
$decodedData = json_decode($data);

for ($i=0; $i<count($decodedData); $i++) {
    if ($decodedData[$i]->username == $_SESSION['username']) {
        
        $decodedData[$i]->balance = $decodedData[$i]->balance - $amount;
    
        $data4 = array("name"=>$decodedData[$i]->name, "email"=>$decodedData[$i]->email, 
        "username"=>$decodedData[$i]->username, "password"=>$decodedData[$i]->password, 
        "wallet_address"=>$decodedData[$i]->wallet_address, "balance"=>$decodedData[$i]->balance);

        $options4 = array(
            'http' => array(
                'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                'method'  => 'PUT',
                'content' => http_build_query($data4)
            )
        );

        $context4  = stream_context_create($options4);
        $result_users = file_get_contents($url, false, $context4);
    }
}

我从代码中删除了最后两行,现在没有显示警告。但平衡的价值仍然没有改变


感谢您的回答。

听起来URL要么不正确,要么无法从运行PHP的地方访问。您是否在承载URL的同一台计算机上运行此代码?侦听该URL的Web服务器是否已启动并正在运行?URL是否正确。我在url所在的同一台机器上运行代码。我已经成功地使用了POST方法,比如
'method'=>'POST',
我从代码中删除了最后两行,现在警告没有显示。但余额的值仍然没有改变。您是否碰巧在我的php.ini中设置了错误报告(error\u reporting)设置?
error\u reporting=E\u ALL&~E\u DEPRECATED&~E\u STRICT
display\u errors=On
Warning: file_get_contents(http://localhost:4000/users): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in ....