2

I am using PHP + CURL to fetch data from a server in one of my actions. I then return the data as json from my action.

My action looks like this

public function executeTest(sfWebRequest $request)
{
 $json = $this->getServerResponse(); // fetches data using CURL
 $this->getResponse()->setContentType('text/json');
 return $this->renderText($json);
}

When the above action is executed, the received json strng is (for example):

{ 'ok': true }1

If I change the last line in the action above to return $this->renderText('foo');

the returned JSON is:

{ 'ok': true }foo

If I change the last line in the action above to return $this->renderText('');

the returned JSON is:

{ 'ok': true }

My question are:

  1. Why is the JSON data from the server being displayed together with the text in my renderText() method?

  2. Where is the '1' appended to the JSON data coming from?

  3. How do I resolve/fix this issue ?

I am running Symfony 1.4.x on Ubuntu

asked Feb 10, 2011 at 15:01
2
  • Well, I'm no expert, but I know renderText appends its passed value to any existing response content, rather than replacing it, so it almost looks as though the json is already there in your sfResponse's existing content, and you're appending $json to it, with $json not being what you expect it to be. We might need to see the source for getServerResponse() to figure out what's going on... Commented Feb 10, 2011 at 15:36
  • @Matt - I think you're on the right track. Looks like the getServerResponse function sets the response text and then returns true. This accounts for the 1 being appended (rendering true shows as a 1), the string foo and the empty string. All of which are being appended. @OP - You should really get getServerResponse to return the response rather than setting it and then returning true. Commented Feb 10, 2011 at 15:45

1 Answer 1

2

From the looks of it, your problem lies in getServerResponse(). Can't help more without seeing that function.

answered Feb 10, 2011 at 15:37

1 Comment

yes actually - you're right. I am returning the return code from curl_exec (which is 1).

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.