James Mathieson
James Mathieson

Reputation: 439

Foreach not firing, string not true array

I have a comma separated list of numbers from a post variable, which outputs e.g:

123, 456, 789, 101, 112 // Number comma space

I then use the following code to process these IDs individually:

$id_string = $_POST['ids'];


$id_array = array_map('trim', explode(',', $id_string));

    foreach ($id_array as $value){
        $url = 'http://myserver.com';
        $data = array('a' => $value, 'reStock' => 'true');
        $get = array();

            foreach($data as $k => $v){
            $get[] = $k . '=' . urlencode($v);
            }

            $get = implode('&', $get);

            $opts = array('http' =>
            array(
            'method'  => 'GET',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => $get
            )
            );

        $context = stream_context_create($opts);
        $mxsendstock = file_get_contents($url, false, $context);
    }

After testing all afternoon, I can't get the foreach to work - nothing happens. The only possible cause I can see is if I'm handling the comma-separated list correctly.

Any ideas?

Upvotes: 0

Views: 73

Answers (1)

bystwn22
bystwn22

Reputation: 1794

is there any problem using that like this?

<?php
  $id_string  = $_POST['ids'];
  $id_array   = array_map( 'trim', explode( ',', $id_string ) );

  foreach( $id_array as $value ) {
    $url  = 'http://myserver.com/';
    $data = array( 'a' => $value, 'reStock' => 'true' );
    $get  = http_build_query( $data );

    $mxsendstock  = file_get_contents( $url."?".$get );
    print( htmlspecialchars( $mxsendstock ) );
  }
?>

Upvotes: 1

Related Questions