Foreach Loop & XML-RPC: it should work, but it is not, why?

I could not find the question that has already been given here in relation to my situation. From my understanding of what this code does, it should work, but I just don’t understand why it is not.

I am reading the SKU list from a text file. Each SKU is on its own line. All I have to do is pass the SKU only to my XML-RPC request and print the results. From the code I paste below, it seems to intersect the SKU, but only one valid result is returned.

If I pass each SKU manually through the $ request variable, I get valid results. I tested 5 SKUs, but I have 200 so that it actually feels so good if I could make it work. I even pulled out Lydia PHP tutorials to see if I understood something, but it didn't seem to me that my code was wrong.

I appreciate your help with this.

<?php

$list = file("skus2.txt");
print_r($list);

foreach ($list as $sku){

  $server_url = "http://xxxx.xxx/webservices/index.php";
  $request = xmlrpc_encode_request("catalog.findProductImagesBySku", array($sku));
  $context = stream_context_create(array(
  'http' => array(
  'method' => "POST",
  'header' => "Content-Type: text/xml",
  'content' => $request)));
   $file = file_get_contents($server_url, false, $context);
   $response = xmlrpc_decode($file);
   print_r ($response);
      }
?>

I tried it every way he feels. I was with him for hours and hours.

This is what I get in return. As you can see its failure in the first 4, even if from a print statement, it grabs the right SKU and successfully works in the last SKU. All SKUs are valid because I tested them one at a time.

    Array
(
    [0] => DJ750605

    [1] => TO88116

    [2] => TO1112516

    [3] => TO1112506

    [4] => ENAI200006
)

//start of 1st run of foreach loop
DJ750605

Warning:  file_get_contents(http://xxx/webservices/index.php) [function.file-get-contents]: failed to open stream: HTTP request failed! ...

//start of 2nd run of foreach loop
TO88116

Warning:  file_get_contents(http://xxx/webservices/index.php) [function.file-get-contents]: failed to open stream: HTTP request failed!...


//start of 3rd run of foreach loop
TO1112516

Warning:  file_get_contents(http://xxx/webservices/index.php) [function.file-get-contents]: failed to open stream: HTTP request failed!...


//start of 4th run of foreach loop
TO1112506

Warning:  file_get_contents(http://xxx/webservices/index.php) [function.file-get-contents]: failed to open stream: HTTP request failed!...

//start of 5th run of foreach loop
ENAI200006Array
(
[0] => Array
    (
        [sku] => ENAI200006
        [large_url] => http://xxxENAI200006.JPG
        [medium_url] => http://xxxENAI200006.JPG
        [thumb_url] => http://xxxENAI200006.JPG
        [url] => http://xxxENAI200006.JPG
    )

)

UPDATE: Working code :)

<pre>
<?php

$list = file('skus2.txt', FILE_IGNORE_NEW_LINES);
print_r($list);

foreach ($list as $sku){

$server_url = "http://xxx.com/xxx/webservices/index.php";
$request = xmlrpc_encode_request("catalog.findProductImagesBySku", array($sku));
$context = stream_context_create(array(
'http' => array(
'method' => "POST",
'header' => "Content-Type: text/xml",
'content' => $request)));
$file = file_get_contents($server_url, false, $context);
$response = xmlrpc_decode($file);
print_r ($response);
}
?>
</pre>

Thanks again, Hooray!

+3
source share
1 answer

( "skus2.txt" ), :

:

     

, FILE_IGNORE_NEW_LINES, rtrim(), , .

:

$list = file('somefile.txt', FILE_IGNORE_NEW_LINES);

, , , :

$list = array('TO88116',
              'TO1112516',
              'etc...'
             );

, .

!

0

All Articles