我正在尝试将网址从 php 页面发送到 json 页面的
格式"http://myserver.com/login?q={"myid":"phill","password":"mypass"}"
.如果我将其粘贴到浏览器地址中,它可以正常工作。我尝试过HttpRequest和cURL,但没有成功。你能建议如何实现这一目标吗?
<?php
$yourJSONEncodedData = array('userid' => "phill", 'password' => "mypassword");
$url = "http://myserver.com/login?q=".json_encode($yourJSONEncodedData);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
$output = curl_exec($curl);
curl_close($curl);
var_dump($output);
$response = json_decode($output,true);
echo $response;
?>
我希望"{}"返回到 php 程序。
我不知道
你为什么要以json格式发送参数?可以在查询字符串中发送参数,并在 json 页中获取所有值。
在 PHP 中使用json_encode()
。
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
上面的例子将输出:{"a":1,"b":2,"c":3,"d":4,"e":5}
使用 cURL 发布 JSON 数据(类似这样)
$url="http://myserver.com/login?q=".$yourJSONEncodedData;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
$output = curl_exec($curl);
curl_close($curl);
var_dump($output);
这就是我可以在不看到您的代码的情况下建议的全部内容。