用php发布json对象(http Post)?
我需要用HTTPPOST请求发布json对象并处理响应 json对象:用php发布json对象(http Post)?,php,json,Php,Json,我需要用HTTPPOST请求发布json对象并处理响应 json对象: { "my_json" : "12345" } 我写了一些这样的东西,但这行不通 $url = "http://localhost/my_json.json"; $json_Data = file_get_contents($url,0,null,null); print_r($json_Data); 而且它不打印任何东西 请帮忙 $url = "http://localhost/my_json.json";
{
"my_json" : "12345"
}
我写了一些这样的东西,但这行不通
$url = "http://localhost/my_json.json";
$json_Data = file_get_contents($url,0,null,null);
print_r($json_Data);
而且它不打印任何东西
请帮忙
$url = "http://localhost/my_json.json";
$json_Data = file_get_contents($url,0,null,null);
$new = json_decode($json_Data);
print_r($new);
我认为这可能会做到客户:
<?php
$data = array('foo' => 'bar', 'red' => 'blue');
$ch = curl_init();
$post_values = array( 'json_data' => json_encode( $data ) );
curl_setopt($ch, CURLOPT_URL, 'http://localhost/server.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_values);
$data = curl_exec($ch);
if(!curl_errno($ch))
{
echo 'Received raw data' . $data;
}
curl_close($ch);
?>
服务器(Server.php):
试试这个:
$jsonFile = 'http://localhost/my_json.json';
$jsonData = file_get_contents($jsonFile);
$phpData = json_decode($jsonData);
print_r($phpData);
问题可能来自文件\u get\u内容的无关参数:
- 2d参数应该是布尔值,并且是可选的(默认值为false)
- 第三个参数正常
- 第四个应为整数,可选(默认值为-1)
$json\u Data=file\u get\u contents($url)代码>
此外,要在浏览器中查看数据,您应该尝试使用标题('Content-type:text/plain')代码>在使用print_r()输出之前,以便浏览器不会进行任何处理
为了确保没有任何东西发送到您的浏览器,您也可以尝试FireFox+FireBug查看HTTP回复…您确定您的web服务器正在正确地服务/my_json.json吗?您的php.ini文件中是否启用了allow_url\u fopen
,以允许通过文件获取内容()
等请求url?请参阅启用了allow\u url\u fopen。如何确保我的web服务器正常运行?你的意思是什么?将浏览器指向并查看是否收到文件。此外,如果您试图发布(从HTTP POST的意义上讲),那么使用curl可能会更好。我支持Ramon的说法,curl可能更好,但我创建了动态json对象。然后将该对象指向另一个url另一个url。'/my_json.json'而不返回:(不要误解我的意思,但你到底想做什么?根据你的问题“我需要发布一个带有HTTP POST请求的json对象并处理响应。”不,没关系,可能问题是我将json对象发送到https,我需要从php文件中的httpsI need中检索对象,以发布对象并从HttpSt检索它。这是我创建json obj$jsob=array();$jsob['my_json']=“12345”$some_data=json_encode($jsob)的方式;您确定可以使用web浏览器访问吗?@user303832:您确定服务器上的设置正确吗?此代码100%有效。
$jsonFile = 'http://localhost/my_json.json';
$jsonData = file_get_contents($jsonFile);
$phpData = json_decode($jsonData);
print_r($phpData);