3

I wasn't able to find a question already answered on here that applied to my situation. From my understanding of what this code is doing, it should actually be working but I just don't understand why it isn't.

I'm reading a list of SKUs from a text file. Each SKU is on it's own line. All I need to do is pass the SKU alone to my XML-RPC request and have it print the results. From the code I'm pasting below, It appears to loop through the SKUs but only one valid result is returned.

If I pass each SKU manually through the $request variable, I get valid results. I've been testing with 5 SKUs but I have 200 to actually do so it would be nice if I can get this to work. I even broke out the Lydia PHP training videos to see if I didn't understand something but it didn't seem like 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 have tried this every which way it feels like. I've been at it for hours and hours.

This is what I'm getting in return. As you can see its failing on the first 4 even though from the print statement, it's grabbing the right SKU and it's successful on the last SKU. All the 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, Cheers!

Tsanders
  • 71
  • 9
  • 1
    file_get_contents(http://xxx/webservices/index.php) [function.file-get-contents]: failed to open stream: HTTP request failed!... <= those ... are _important_! It is very likely the solution itself. (Say, it's 406, then the remote has mod_security etc) – chx May 14 '11 at 07:23
  • If you can, I've found using xdebug in development invaluable in diagnosing issues with rpc/soap calls. – David May 16 '11 at 21:54

1 Answers1

0

I'm suspicious of the call to file('skus2.txt'), as stated in the manual:

Note:

Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used, so you still need to use rtrim() if you do not want the line ending present.

I would try either calling like this:

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

Or even just passing the array manually to see if it works, ie:

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

You at least reduce the scope of what you have to debug this way.

Good-luck!

Stephane Gosselin
  • 9,030
  • 5
  • 42
  • 65
  • Thanks stefgosselin. I missed your reply back when you wrote it. I gave up. :) Now I'm ready to tackle this again and I'll try your suggestions. – Tsanders Nov 25 '11 at 16:56
  • Eureka!!! It works if I add FILE_IGNORE_NEW_LINES like you suggested! The output is hideous but it grabbed all of the info for each sku in the list. :) Thanks so much! Now I can really use all of the tools available to communicate with the supplier's server. – Tsanders Nov 25 '11 at 20:19