1

I am using PHP http_post_data() call to send data to a cakephp controller.I do it like this:

$response=http_post_data($url, $xml_data_encoded);

The data arrives ok to the destination and I get a response which holds the response status.In my case the status is number 1 which means -data delivered ok.As you can see from the code below I get not only the status number (which is at the bottom of the message) but also the whole http post header.How can I strip this message off the header code so that eventually the response message holds only the status number?

HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Date: Thu, 10 Nov 2011 08:34:15 GMT
Server: Apache/2.2.17 (Ubuntu)
X-Powered-By: PHP/5.3.5-1ubuntu7.3
Set-Cookie: CAKEPHP=xxxxxxxxxxxxxxxx; expires=Fri, 18-Nov-2011 16:34:15 GMT;       path=/XXXXXXXXXX/xxxxxxxx
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
Vary: Accept-Encoding
Content-Length: 19
Content-Type: text/html

1

Btw, I also tried this:

 HttpMessage::getBody(http_post_data($url, $xml_data_encoded));

and got no response at all. Any help will be highly appreciated.

1
  • You want only the 1 at the bottom?
    – Mob
    Nov 10, 2011 at 8:55

2 Answers 2

2
$response = http_post_data($url, $data);
preg_match_all('~HTTP/1\.[01]\s(\d{3})~', $response, $codes);

$codes would store all matches. Just print_r the array and look for desired keys.

3
  • Look at the http_parse_headers function as well. It will store 'status' key in the array, so you may either use it as it is, or preg_match to get the status code.
    – Pavel S.
    Nov 10, 2011 at 8:56
  • Sorry for deselection of your answer but it appears not to work.print_r() of the $codes returns only the first row of the response header.The problem is still unsolved
    – Michael IV
    Nov 20, 2011 at 9:06
  • Oh, sorry. Preg_match only searches for the first match. Use preg_match_all to match every possible string.
    – Pavel S.
    Dec 6, 2011 at 16:45
2

Instead of a regular expression you can use http_parse_message

$response = http_parse_message(http_post_data($url, $data));
print $response->body;
1
  • or just the HttpSocket class of CakePHP
    – mark
    Nov 20, 2012 at 19:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.