Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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代码中加载json文件以准备HTTP POST?_Php_Xml_Json_Http Post - Fatal编程技术网

如何在php代码中加载json文件以准备HTTP POST?

如何在php代码中加载json文件以准备HTTP POST?,php,xml,json,http-post,Php,Xml,Json,Http Post,我在磁盘上有一个.json文件。我想把它加载到我的php代码中,这样我就可以用它发出HTTP POST请求 我对XML做了完全相同的处理,如下所示: $file = 'http://localhost/myServer/test.xml'; $xml_builder = simplexml_load_file($file)) 然后我使用$xml\u builder变量发送带有CURL的xml 如何使用JSON实现完全相同的功能?试试这个 $file = '/path/to/json.json'

我在磁盘上有一个.json文件。我想把它加载到我的php代码中,这样我就可以用它发出HTTP POST请求

我对XML做了完全相同的处理,如下所示:

$file = 'http://localhost/myServer/test.xml';
$xml_builder = simplexml_load_file($file))
然后我使用
$xml\u builder
变量发送带有CURL的xml

如何使用JSON实现完全相同的功能?

试试这个

$file = '/path/to/json.json';
$json = file_get_contents($file);
$data = json_decode($json, true);
它将生成一个数据数组,您可以随意使用该数组

尝试:

$url = 'URL GOES HERE';
$file = file_get_contents('http://localhost/myServer/test.xml');

// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $url,
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        'xml' => $file
    )
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);

//Print response
print_r('<pre>');
print_r($resp);
die();
$url='url在这里';
$file=文件获取内容('http://localhost/myServer/test.xml');
//获取卷曲资源
$curl=curl_init();
//设置一些选项-我们在这里也传递一个useragent
curl_setopt_数组($curl,数组(
CURLOPT_RETURNTRANSFER=>1,
CURLOPT_URL=>$URL,
CURLOPT_POST=>1,
CURLOPT_POSTFIELDS=>数组(
'xml'=>$file
)
));
//将请求和保存响应发送到$resp
$resp=curl\u exec($curl);
//关闭请求以清除某些资源
curl_close($curl);
//打印响应
印刷品(“”);
印刷费($resp);
模具();

我认为您需要执行以下操作:

$myJSON = file_get_contents('location/of/your/json');
// $myJSON now contains the JSON string. you can already send this with curl.

$myJSONDecoded = json_decode($myJSON);
// now $myJSONDecoded holds an array with the data. which you can use any way you like.

您需要对其进行解码,因为您在
$variable
中将其作为字符串获取,然后根据需要使用它

您是否尝试在php文档或google上查找“json”?这不是一个具体的编程问题,您甚至还没有写出您的问题的具体内容。我们不知道这到底意味着什么,你在过去的XML中做了什么,所以我们可以告诉你同样的事情。如果您正在寻找如何读取文件,请参考PHP手册以获得指导,没有必要在此处为此类内容编写问题。为什么要对其进行解码?我希望它是一个json字符串,对吗?我想以JSONI的形式发送,在这种情况下,不要使用解码行。仅在代码的前2部分添加了CURL部分,您可能需要将字段名称更改/添加到CURLOPT_POSTFIELDS中,以更改/添加到文章的字段名称。
$myJSON = file_get_contents('location/of/your/json');
// $myJSON now contains the JSON string. you can already send this with curl.

$myJSONDecoded = json_decode($myJSON);
// now $myJSONDecoded holds an array with the data. which you can use any way you like.