title: Executing Observer sales_order_place_after after the order has been put in the database
body: <p>I'm creating a Magento plugin in which I made a Observer script which I get the order info and product info in order to do a request to a API outside of magento. The thing is, when the script gets executed, the data hasn't been put into the database yet by Magento. Simply sleeping the script for x seconds does not help (ofcourse) because the whole system gets halted.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>&lt;?php
  //require_once("DelayedDatabaseAccess.php");
  
  class Plesents_Plugin_Model_Observer {
      public function plesentsRedirect(Varien_Event_Observer $obs) {
          //require_once("DelayedDatabaseAccess.php");
          ob_start();
  
  
          $data = $obs-&gt;getEvent()-&gt;getOrder()-&gt;getData();
          $ent_id = $data['entity_id'];
          mail("jeffhuys@gmail.com", "Yeah!", $data['increment_id'] . " || " .     $data['entity_id'] . ".. Waiting 10 seconds..");
  
          //$data = $obs-&gt;getEvent()-&gt;getOrder()-&gt;getData();
          //$result = ob_get_clean();
  
  
          //$ent_id = $data['entity_id'];
          //$ent_id = $_POST['entity_id'];
  
  
          //DB Connectie voor DB Magento
          $con =  mysql_connect("xxxx", "xxxx", "xxxx");
                  mysql_select_db("xxxx");
  
          // Query voor dataopvraag
          // TODO pas hier enorm op! Volgens mij is dit SQL-injectie-gevoelig!!!!
          // Stel dat iemand als email "jeffhuys@gmail.com' DROP TABLE x" doet, bam.
          echo($ent_id);
          $sql = "SELECT o.base_subtotal, o.increment_id, o.customer_email,     o.customer_firstname, o.customer_lastname, o.shipping_amount FROM mag1_sales_flat_order o,     mag1_sales_flat_order_item i, mag1_catalog_product_flat_1 f WHERE o.entity_id=i.order_id     AND o.entity_id='107' AND i.name=f.name ORDER BY o.increment_id LIMIT 1";
  
          $sql2 = "SELECT i.name, f.short_description, f.small_image FROM     mag1_sales_flat_order o, mag1_sales_flat_order_item i, mag1_catalog_product_flat_1 f WHERE     o.entity_id=i.order_id AND i.name=f.name AND o.entity_id='107' ORDER BY o.increment_id";
  
          echo($sql . "\n\n");
          echo($sql2 . "\n\n");
  
          $result  = mysql_query($sql);
          echo("  ERROR1?: " . mysql_error());
          $result2 = mysql_query($sql2);
          echo("  ERROR2?: " . mysql_error());
  
          echo("   ROWS:  " . mysql_num_rows($result));
  
  
  
          var_dump($result);
  
          // Static variabeles table: plesents_authentication
              $app_id = 3;
          $authentication_token = "xxxx";
  
  
          // Opbouw basis URL voor Plesents
          $plesentsurl ="http:/xxxxx/api/orders.json";
  
              //Resultaten uit Query in Array
          $row = mysql_fetch_array($result);
          echo("\n\nDATA:");
          var_dump($row);
          //var_dump($data);
  
          $row['base_subtotal']   = $row['base_subtotal'] * 100;
          $row['shipping_amount'] = $row['shipping_amount'] * 100;
  
  
          $sha1=sha1($app_id.$authentication_token.$row['base_subtotal'].$row['increment_id']."https://oege.ie.hva.nl/~hilgern001/magento/notify.php".$row['customer_firstname']." ".$row['customer_lastname'].$row['customer_email']);
      echo("Sha1 calc: " . $sha1);
  
  
          $httpacc = array(
            "authentication_token" =&gt; $authentication_token,
        "order" =&gt; array(
          "price_in_cents"     =&gt; $row['base_subtotal'],
          "notify_url"         =&gt; "https://xxxxx/magento/notify.php",
          "user_name"          =&gt; $row['customer_firstname']." ".$row['customer_lastname'],
          "user_email"         =&gt; $row['customer_email'],
          "order_id"           =&gt; $row['increment_id'],
          "test"               =&gt; true,
          "sha1"               =&gt; $sha1
          )
      );
      $product_lines = array();
          while($row2 = mysql_fetch_array($result2)) {
          $product_lines[] = array(
            "name"             =&gt; $row2['name'],
            "description"      =&gt; $row2['short_description'],
            "remote_image_url" =&gt; "https://xxxx/magento/media/catalog/product/cache/1/small_image/170x/9df78eab33525d08d6e5fb8d27136e95".$row2['small_image']
          );
      }  
      $httpacc['order']['details_attributes'] = $product_lines;
  
      $pathdirectory = json_encode($httpacc);
  
      //URL opbouwen en escapen (voor debugging)  
      //echo $pathdirectory;
  
      $ch = curl_init($plesentsurl);  
  
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
      curl_setopt($ch, CURLOPT_POSTFIELDS, $pathdirectory);                                                                  
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
      curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
          'Content-Type: application/json',                                                                                
          'Content-Length: ' . strlen($pathdirectory))                                                                       
      );                                                                                                                   
  
      $result = curl_exec($ch);
      $redirect_result = json_decode($result, true);
      $redirect_result_staging = explode('.', $redirect_result['redirect_url']);
      $redirect_url_staging = "https://staging." . $redirect_result_staging[1] . "." . $redirect_result_staging[2];
      $sha_id = $redirect_result['id'];
      $sha1_validate = sha1($app_id.$authentication_token.$sha_id.$row['base_subtotal'].$row['increment_id']."https://xxxx/magento/notify.php".$row['customer_firstname']." ".$row['customer_lastname'].$row['customer_email']);
  
      echo("\n\nsha1_validate: " . $sha1_validate);
      echo("\n\nredirect_result: " . $redirect_result['sha1']);
  
      var_dump($result);
  
      if($redirect_result['sha1']==$sha1_validate) {
          //echo '&lt;meta http-equiv="refresh" content="0;URL='.$redirect_url_staging.'" /&gt;';
          $ch = curl_init($redirect_url_staging);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
          $result = curl_exec($ch); 
      } 
      else {
          echo "sha1 could not be validated correctly!";
      }
  
      $pagerender = ob_get_clean();
      mail('jeffhuys@gmail.com', 'pagerender', $pagerender);
  
      //$res = ob_get_clean();
      //mail("jeffhuys@gmail.com", "Yeah!", "Magento moves forward.\n " . $res);
  }
  }
  
  ?&gt;
  </code></pre>
  
  <p>I know I can use mage to get the info of the order from, but I can't use that to get product info (which I absolutely need).</p>
  
  <p>Is there any way to let this code be executed AFTER Magento inserts his info into the database?</p>
  
  <p>Thanks so much.</p>
  
  <p><strong>EDIT: Sorry, forgot to add. In the SQL-querys, entity_id='107' works, but when I make this dynamic (entity_id=$_POST['entity_id'] or something like that), it will NOT work because this requests data not yet available.</strong></p>