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.

1
  • Show your code and we'll see what happening, without it, we cannot do too much Commented Sep 16, 2013 at 13:24

2 Answers 2

2

I don't know why you are sending parameter in json format? You can send parameters in query string and get all the values in json page.

Sign up to request clarification or add additional context in comments.

1 Comment

Do you mean I should use HttpRequest?. I am new to json and am not sure how to get the values in json page.
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.

1 Comment

Thank you for your answer. It is what I have been trying, but I still get the same response. string(307) "HTTP/1.1 200 OK Cache-Control: no-cache, must-revalidate Set-Cookie: session=52371dfc03643ec12bcf53ad;Path=/ Expires: Thu, 01 Jan 1970 00:00:00 GMT Content-Type: application/json;charset=UTF-8 Content-Encoding: gzip Transfer-Encoding: chunked Server: Jetty(8.1.10.v20130312) ‹«®C¿¦£". I was expecting "{}" to be returned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.