【发布时间】:2013-06-19 15:08:31
【问题描述】:
我已经创建了一个脚本来发送 json 数据到一个 url
$data = array();
$key = 'mysecretkey';
$string = 'teststring';
$data['date'] = '2013-06-19 05:38:00';
$data['encrypted_string'] = mcrypt_encode($string,$key);
$data['id'] = 231;
$data['source_ref'] = 'testreference';
$data['status'] = 'Active';
header('Content-type: text/json');
header('Content-type: application/json');
$url = 'http://localhost/myproject/test/add_json_data';
$json_string = json_encode($data);
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS,$json_string);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
mcrypt_encode 对字符串进行编码。这是json_string
{
"date": "2013-06-19 05:38:00",
"encrypted_string": "7Y4jqgIfhj25ghsF8yJ/qTtzXafTtIlwsz7xWIDVWJGoF22X2JbfSWfQtgmI1dYyyJDgs3nmaWctTEgKW5VmHw==",
"id": 231,
"source_ref": "testreference",
"status": "Active"
}
当我执行脚本时,我提供的 url 上没有任何反应。
这是我在网址上所做的。
function add_json_data()
{
$json = file_get_contents('php://input');
$obj = json_decode($json);
$this->load->helper('file');
write_file('json.json',$json);
}
我正在使用 codeigniter,所以我只是将发布的数据保存在 json 格式的文件中,看看会发生什么。但我看到没有调用 url。我假设由于包含编码字符串的编码 json 字符串,它不会将数据发送到 url。如何将我的编码密钥发送到 url。我还检查了是否将encrypted_string 替换为“测试”,它工作正常。我如何将数据发送到 url 或任何替代方案?请帮忙。
【问题讨论】:
-
你启用了 mcrypt php 模块吗?也许您在关闭错误报告的情况下调用未定义函数?您的代码在我的本地运行良好...
-
我不明白你的意思。你能解释一下吗?
标签: php json codeigniter curl encode