0

I am trying to send a url from a php page to a json page in the format

"http://myserver.com/login?q={"myid":"phill","password":"mypass"}"

. If I paste this into a browser address it works correctly. I have tried HttpRequest and cURL with no success. Can you suggest how this may be achieved?.

<?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;
?>

I am expecting "{}" to be returned to the php program.

4

2 回答 2

2

我不知道您为什么要以json格式发送参数?您可以在查询字符串中发送参数并获取 json 页面中的所有值。

于 2013-09-16T13:28:49.027 回答
1

Make use of json_encode() in PHP.

<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

echo json_encode($arr);
?>

The above example will output: {"a":1,"b":2,"c":3,"d":4,"e":5}

Posting the JSON data using cURL (Something like this)

$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);

That's all i can suggest without seeing your code.

于 2013-09-16T13:21:26.737 回答