Friday, 15 February 2013

PHP script returning 500 error - Using cURL to send to API -



PHP script returning 500 error - Using cURL to send to API -

i working api update our product information. api allows me mass item upload of 20 items per call. in order update 1000+ items have loop through script number of times. script compiles xml of 20 items , uses curl send it, grabs next 20 items creates xml , repeat. when limit outter loop to, 15, works fine , returns success message each api call. fails @ random times i've had send 40 api calls before failing , i've had fail after 10 calls.

i can't find in error logs why getting 500 back. i've reached out folks managing api see if have idea.

my best guess has timeout.

$query = "select distinct `id`, `link`, `price`, `ship_group`, `fixed_ship`, `do_not_sell`, `shipping_weight`, `shipping_length`, `shipping_width`, `shipping_height`, `title`, `brand`, `inventory`, `available`, `type`, `min_order_qty`, `image`, `call`, `allow_in_cart`, `allow_backorder` $table `id` > 10001 , `price` > 0 , `base_match` = 'm' order `id`"; $resultid = mysql_query($query, $linkid) or die(mysql_error()); //total number of rows divided 20 rounded nearest whole number //call createitems amount of times $cycles = ceil(mysql_num_rows($resultid) / 20); $i = 0; while( $i < 20 ){ //tried sleeping between calls didnt help sleep(2); $i++; $x = 0; $xml_output = "\t<items>\n"; while( $x < 20){ $row = mysql_fetch_assoc($resultid); $x++; $itemarray[$x]['id'] = $row['id']; if ($itemarray[$x]['id'] == "") { break; }; $itemarray[$x]['link'] = $row['link']; $itemarray[$x]['price'] = $row['price']; $itemarray[$x]['ship_group'] = $row['ship_group']; $itemarray[$x]['fixed_ship'] = $row['fixed_ship']; $itemarray[$x]['do_not_sell'] = $row['do_not_sell']; $itemarray[$x]['shipping_weight'] = $row['shipping_weight']; $itemarray[$x]['shipping_length'] = $row['shipping_length']; $itemarray[$x]['shipping_width'] = $row['shipping_width']; $itemarray[$x]['shipping_height'] = $row['shipping_height']; $itemarray[$x]['title'] = $row['title']; $itemarray[$x]['brand'] = $row['brand']; $itemarray[$x]['inventory'] = $row['inventory']; $itemarray[$x]['type'] = $row['type']; $itemarray[$x]['min_order_qty'] = $row['min_order_qty']; $itemarray[$x]['available'] = $row['available']; $itemarray[$x]['image'] = $row['image']; $itemarray[$x]['call'] = $row['call']; $itemarray[$x]['allow_in_cart'] = $row['allow_in_cart']; $itemarray[$x]['allow_backorder'] = $row['allow_backorder']; $desc = htmlspecialchars_decode($itemarray[$x]['title']); $desc = str_replace('"',"'", $desc); $desc = str_replace('&',"and", $desc); $desc = str_replace('%',"%25", $desc); $track_inventory = (strtolower($itemarray[$x]['inventory']) == "i") ? true : false; $allow_backorder = ($itemarray[$x]['allow_backorder'] == 0)? true : false; $cost = $itemarray[$x]['price']; if ($itemarray[$x]['do_not_sell'] == 1) { $inactive = true; $cost = 0.00; } else if (!$allow_backorder && $itemarray[$x]['available'] <= 0) { $cost = 0.00; $inactive = true; } else if ($itemarray[$x]['call'] == 1 ) { $cost = 0.00; $inactive = true; } else if ($itemarray[$x]['allow_in_cart'] == 0) { $inactive = true; // phone call set no // allow in cart set no // shows cost dont allow in cart } else if ($track_inventory && $itemarray[$x]['available'] <= 0) { $inactive = false; //allow in cart //display shipping delay message //do nil in uc } else { $inactive = false; }; $xml_output .= "\t\t<item>\n"; $xml_output .= "\t\t\t<merchant_item_id>".$itemarray[$x]['id']."</merchant_item_id>\n"; $xml_output .= "\t\t\t<description>".$desc."</description>\n"; $xml_output .= "\t\t\t<view_url>".htmlspecialchars($itemarray[$x]['link'])."</view_url>\n"; $xml_output .= "\t\t\t<cost>".number_format($cost, 2, '.', '')."</cost>\n"; $xml_output .= "\t\t\t<uom_weight>lb</uom_weight>\n"; $xml_output .= "\t\t\t<weight>".number_format($itemarray[$x]['shipping_weight'], 2, '.', '')."</weight>\n"; $xml_output .= "\t\t\t<inactive>".var_export($inactive, true)."</inactive>\n"; $xml_output .= "\t\t\t<minimum_quantity>".$itemarray[$x]['min_order_qty']."</minimum_quantity>\n"; $xml_output .= "\t\t\t<inventory_quantity>".$itemarray[$x]['available']."</inventory_quantity>\n"; $xml_output .= "\t\t\t<track_inventory>false</track_inventory>\n"; $xml_output .= "\t\t\t<manufacturer_name>".htmlspecialchars($itemarray[$x]['brand'])."</manufacturer_name>\n"; $xml_output .= "\t\t\t<manufacturer_sku></manufacturer_sku>\n"; $xml_output .= "\t\t\t<uom_distance>in</uom_distance>\n"; // if solar panel or if weighs on 70lbs remove dimensions //add per dustins request - "should prepare panel shipping errors" if (strtolower($itemarray[$x]['type']) == "solar panel" || $itemarray[$x]['shipping_weight'] >= 70) { $xml_output .= "\t\t\t<length>0</length>\n"; $xml_output .= "\t\t\t<width>0</width>\n"; $xml_output .= "\t\t\t<height>0</height>\n"; } else { $xml_output .= "\t\t\t<length>".number_format($itemarray[$x]['shipping_length'], 2, '.', '')."</length>\n"; $xml_output .= "\t\t\t<width>".number_format($itemarray[$x]['shipping_width'], 2, '.', '')."</width>\n"; $xml_output .= "\t\t\t<height>".number_format($itemarray[$x]['shipping_height'], 2, '.', '')."</height>\n"; } $xml_output .= "\t\t\t<froogle>\n"; $xml_output .= "\t\t\t\t<image_url>".$itemarray[$x]['image']."</image_url>\n"; $xml_output .= "\t\t\t</froogle>\n"; $xml_output .= "\t\t\t<shipping>\n"; if ($itemarray[$x]['ship_group'] == strtolower('fixed') && $itemarray[$x]['fixed_ship'] == 0) { $xml_output .= "\t\t\t\t<free_shipping>true</free_shipping>\n"; } elseif ($itemarray[$x]['ship_group'] == strtolower('freight')) { $xml_output .= "\t\t\t\t<methods>\n"; $xml_output .= "\t\t\t\t\t<method>\n"; $xml_output .= "\t\t\t\t\t\t<name>con-way: ltl</name>\n"; $xml_output .= "\t\t\t\t\t\t<validity>valid for</validity>\n"; $xml_output .= "\t\t\t\t\t</method>\n"; $xml_output .= "\t\t\t\t</methods>\n"; } else { $xml_output .= "\t\t\t\t<free_shipping>false</free_shipping>\n"; } $xml_output .= "\t\t\t</shipping>\n"; $xml_output .= "\t\t</item>\n"; } $xml_output .= "\t</items>\n"; //open xml , write 20 items it. $fh = fopen('./bulk-item-pusher.xml','w') or die($php_errormsg); fwrite($fh, $xml_output) or die($php_errormsg); fclose($fh); $ch = curl_init(); curl_setopt($ch, curlopt_header, 0); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_url, "theapiurl"); curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_postfields, "merchantid=".$merchid."&login=".$login."&password=".$password."&function=".$function."&items=".$xml); $content=curl_exec($ch); if(curl_errno($ch)) { $err = curl_error($ch); $ef = fopen('./last-item.xml','w') or die($php_errormsg); fwrite($ef, $err) or die($php_errormsg); break; } curl_close($ch); echo htmlspecialchars($content ." " . $itemarray[$x]['id'])."<br />"; } echo "finished"; ?>

looks have bug (maybe typo in question?)

curl_setopt($ch, curlopt_postfields, "merchantid=".$merchid."&login=".$login."&password=".$password."&function=".$function."&items=".$xml);

$xml should $xml_output there no? also, unless you're using somewhere else later on, why write ./bulk-item-pusher.xml?

add error handling bit check error responses remote api

// handle curl error if(curl_errno($ch)) { $err = curl_error($ch); $ef = fopen('./last-item.xml','w') or die($php_errormsg); fwrite($ef, $err) or die($php_errormsg); break; } // handle error response api elseif(curl_getinfo($ch, curlinfo_http_code) === '500') { // take note of 20 records sent cause error. // need analyzed more determine 1 caused // remote service crap out }

since timeout doesn't appear problem, i'll sec @jay blanchard's comment above; it's info you're sending on of requests.

at point take each of 20 items sent , send them individually determine if 1 of items you're sending causing remote side crash.

from there determine problem particular info item. i'd follow communication folks maintaining api 1 time figure out. 500's mean "our server couldn't cope request". nice if revise handle such case , send 200 response component of payload indicating items couldn't processed, indexed type of correlation identifier.

php api curl

No comments:

Post a Comment