Jump to content

Recommended Posts

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.

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

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

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.

 

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.

Also, the options need to specify the protocol:

 

$config = array(
   'http' => array(
      'method' => 'POST',
      'content' => http_build_query($data),
      'ignore_errors' => true
   )
);

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!

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.