如何使用PHP执行相同的cURL POST请求

如何使用PHP执行相同的cURL POST请求,php,curl,bottle,Php,Curl,Bottle,我需要从php执行相同的代码。如您所见,在这段代码中,我将json作为上下文。 此代码现在从linux终端执行: curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method":"leer_datos", "params": {"bd":"escuela","tabla":"secretaria","id":"2"}}' http://localhost:8090/rpc/alchemy 我在st

我需要从php执行相同的代码。如您所见,在这段代码中,我将json作为上下文。 此代码现在从linux终端执行:

curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method":"leer_datos", "params": {"bd":"escuela","tabla":"secretaria","id":"2"}}' http://localhost:8090/rpc/alchemy
我在stackoverflow上看到了其他问题和回答,例如

$url = 'http://server.com/path';
$data = array('key1' => 'value1', 'key2' => 'value2');

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

var_dump($result);
但我无法修复它


其思想是将数据传递到一个文件中,而瓶子框架准备接收该数据。这个文件是用终端中的cURL调用编写和测试的。

您考虑过使用PHP的cURL函数吗?你试过http_build_query(json_encode($data))?@Javad,我试过$data=“{'jsonrpc':'2.0','method':'entidades','params':'entidades.json'}”content'=>http_build_query(json_encode($data)),瓶子在rpc.py中运行。我用这段代码用浏览器打开php文件,但什么也没发生。杰伊·布兰查德,我考虑过卷发,但我什么都不懂。