Craig Roberts Posted January 10, 2011 Share Posted January 10, 2011 I'm sure this is a simple question, and I'm not a total newbie, but I keep drawing a blank. In accordance with existing specifications, a client has created a PHP page on their web server which responds to a query by returning a small block of XML. I need to use that XML, and I have no problem doing that using XMLReader->open(clientURL). I can call the client's PHP page and get XML back that I can parse and everything is wonderful. Except for one thing: I need to send a product ID when I call the client's PHP page, and I need to send it using the POST method. I guess that means my clean and neat one-step process is going to have to become a two-step process, and I'm going to have to use some other means besides XMLReader->open() to retrieve the XML. Can sombody recommend the best way to do this? I need to send POST data first to get the correct XML response, and then I need to capture the XML in a way that allows me to parse it. Quote Link to comment https://forums.phpfreaks.com/topic/223979-how-to-send-post-data-and-process-returned-xml/ Share on other sites More sharing options...
ignace Posted January 10, 2011 Share Posted January 10, 2011 Untested function post($url, $content) { // http://www.php.net/manual/en/context.http.php $config = array( 'method' => 'POST', 'ignore_errors' => true ); $stream = stream_context_create($config); $fopen = fopen($url, 'r+', false, $stream); $response = ''; fwrite($fopen, http_build_query($content)); while($chunk = fread($fopen, 1024)) { $response .= $chunk; } fclose($fopen); $reader = new XMLReader(); $reader->xml($response); return $reader; } Quote Link to comment https://forums.phpfreaks.com/topic/223979-how-to-send-post-data-and-process-returned-xml/#findComment-1157473 Share on other sites More sharing options...
Craig Roberts Posted January 10, 2011 Author Share Posted January 10, 2011 Thanks for the response ignace. So, does that mean $response will be an XML object? Can I move through that and grab text node values using XMLReader? Quote Link to comment https://forums.phpfreaks.com/topic/223979-how-to-send-post-data-and-process-returned-xml/#findComment-1157484 Share on other sites More sharing options...
Craig Roberts Posted January 10, 2011 Author Share Posted January 10, 2011 Oops, never mind ignace, I see that the answer to my question is in your sample code. I must not have scrolled down all the way. Thanks again. I'll try it and let you know. Quote Link to comment https://forums.phpfreaks.com/topic/223979-how-to-send-post-data-and-process-returned-xml/#findComment-1157490 Share on other sites More sharing options...
Craig Roberts Posted January 10, 2011 Author Share Posted January 10, 2011 I haven't got this to work so far. Can you tell me what you would expect to be passed as the $content parameter? I assigned it the value "sku=K1111", which is the product ID I need to pass. Should that work? Quote Link to comment https://forums.phpfreaks.com/topic/223979-how-to-send-post-data-and-process-returned-xml/#findComment-1157496 Share on other sites More sharing options...
ignace Posted January 10, 2011 Share Posted January 10, 2011 Try this: function post($url, array $data) { // http://www.php.net/manual/en/context.http.php $config = array( 'method' => 'POST', 'content' => http_build_query($data), 'ignore_errors' => true ); $stream = stream_context_create($config); $response = file_get_contents($url, false, $stream); $reader = new XMLReader(); $reader->xml($response); return $reader; } $xmlReader = post('http://domain.top/resource', array('foo' => 'bar')); Quote Link to comment https://forums.phpfreaks.com/topic/223979-how-to-send-post-data-and-process-returned-xml/#findComment-1157499 Share on other sites More sharing options...
Craig Roberts Posted January 10, 2011 Author Share Posted January 10, 2011 This is almost working. I'm getting a response back from the client's PHP page and I'm able to parse it with XMLReader. But it's still giving me the default values as if I were not passing the product ID as a parameter. I'm not sure why that would be. Here's what I have: $url = $_POST['url']; $data = array('product_id' => 'K1111'); $config = array( 'method' => 'POST', 'content' => http_build_query($data), 'ignore_errors' => true ); $stream = stream_context_create($config); $response = file_get_contents($url,false,$stream); $reader = new XMLReader(); $reader->xml($response); This is very close to working. I wish I knew what was wrong. I've checked and the parameter name is correct. Quote Link to comment https://forums.phpfreaks.com/topic/223979-how-to-send-post-data-and-process-returned-xml/#findComment-1157539 Share on other sites More sharing options...
AbraCadaver Posted January 10, 2011 Share Posted January 10, 2011 It's not going to work with file_get_contents() no matter what you do because it is issuing a GET request. Try modifying ignace's original code using fopen() or try just using http_post_fields(). If available it will be far easier. Quote Link to comment https://forums.phpfreaks.com/topic/223979-how-to-send-post-data-and-process-returned-xml/#findComment-1157545 Share on other sites More sharing options...
AbraCadaver Posted January 10, 2011 Share Posted January 10, 2011 Also, the options need to specify the protocol: $config = array( 'http' => array( 'method' => 'POST', 'content' => http_build_query($data), 'ignore_errors' => true ) ); Quote Link to comment https://forums.phpfreaks.com/topic/223979-how-to-send-post-data-and-process-returned-xml/#findComment-1157546 Share on other sites More sharing options...
Craig Roberts Posted January 10, 2011 Author Share Posted January 10, 2011 YES! Your final post, AbraCadaver, was the key. ignace's second suggestion works but only after specifying the 'http' protocol as you indicated. But, having done that, I'm now getting the expected response. file_get_contents apparently uses the POST method if you specify that in the $config array. I don't appear to have the http_post_fields function available on my server or that might have been even easier, I suppose. Anyway, thanks very much to both of you! You guys have been a great help and I appreciate you taking the time! Quote Link to comment https://forums.phpfreaks.com/topic/223979-how-to-send-post-data-and-process-returned-xml/#findComment-1157554 Share on other sites More sharing options...
AbraCadaver Posted January 10, 2011 Share Posted January 10, 2011 YES! Your final post, AbraCadaver, was the key. ignace's second suggestion works but only after specifying the 'http' protocol as you indicated. But, having done that, I'm now getting the expected response. file_get_contents apparently uses the POST method if you specify that in the $config array. Well, that's cool. I'll have to test that and remember it. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/223979-how-to-send-post-data-and-process-returned-xml/#findComment-1157585 Share on other sites More sharing options...
ignace Posted January 10, 2011 Share Posted January 10, 2011 It's not going to work with file_get_contents() no matter what you do because it is issuing a GET request. I am more clever then I appear Quote Link to comment https://forums.phpfreaks.com/topic/223979-how-to-send-post-data-and-process-returned-xml/#findComment-1157623 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.