本文介绍了Salesforce/PHP-出站消息(SOAP)-内存限制问题? DOMDocument :: loadXML()标签问题中的数据过早结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:

好,我知道了,好像fread有一个文件大小限制,将其更改为

OK I figured it out, looks like fread has a filesize limitation, changed this to

file_get_contents('php://input')

,但现在使用SF给出了java.net.SocketTimeoutException:读取超时错误,PHP方面没有任何提示.我还添加了set_time_limit(0);到PHP脚本,如果我了解正确执行脚本的时间,则需要花费很长时间.有什么想法吗?

, but now having SF give a java.net.SocketTimeoutException: Read timed out error and nothing on the PHP side. I have also added set_time_limit(0); to the PHP script which if I understand correctly execute the script for as long as it takes. Any thoughts?

顺便说一句:我最多可以处理25个(经测试),但不能处理100个

BTW: I can process up to 25 (that I've tested) but not 100

我正在使用Salesforce将出站消息(通过SOAP)发送到另一台服务器.服务器一次可以处理大约8条消息,但是如果SOAP请求包含8条以上的消息,则不会发送回ACK文件. SF可以在1个SOAP请求中发送多达100条出站消息,我认为这导致PHP出现内存问题.如果我将出站消息一一处理,它们都可以顺利通过,那么我什至可以一次完成8个,而不会出现任何问题.但是较大的集不起作用.

I'm using Salesforce to send outbound messages (via SOAP) to another server. The server can process about 8 messages at a time, but will not send back the ACK file if the SOAP request contains more than 8 messages. SF can send up to 100 outbound messages in 1 SOAP request and I think this is causing a memory issue with PHP. If I process the outbound messages 1 by 1 they all go through fine, I can even do 8 at a time with no issues. But larger sets are not working.

SF中的错误:

org.xml.sax.SAXParseException: Premature end of file

在HTTP错误日志中,我看到传入的SOAP消息似乎已被切掉,并抛出了PHP警告:

Looking in the HTTP error logs I see that the incoming SOAP message looks to be getting cut of which throws a PHP warning stating:

DOMDocument::loadXML() ... Premature end of data in tag ...

PHP致命错误:

Call to a member function getAttribute() on a non-object

这使我相信PHP存在内存问题,并且由于其大小而无法解析传入的消息.

This leads me to believe that PHP is having a memory issue and can not parse the incoming message due to it's size.

我当时想我可以设置:

ini_set('memory_limit', '64M'); // This has done nothing to fix the problem

但这是正确的方法吗?有没有一种方法可以将其设置为随着传入的SOAP请求动态增加?

But would this be the correct approach? Is there a way I could set this to increase with the incoming SOAP request dynamically?

更新:添加一些代码

 /**
 * To parse out incoming SOAP requests and insert the values into a database table
 * 
 * {@link http://www.mikesimonds.com/salesforce-php-tutorials/95-using-salesforce-outbound-soap-messages-php-2.html}
 */

// Might need more memory?
ini_set('memory_limit', '64M'); // So far this does nothing to help the bulk requests

/**
 * Set the document root path
 * @var $doc_root
 */
$doc_root = $_SERVER['DOCUMENT_ROOT'];


/**
 * This is needed for the $sObject object variable creation
 * found in phptoolkit-11_0 package available from SalesForce
 */
require_once(DOC_ROOT . SALESFORCE_DIRECTORY . SALESFORCE_PHP_TOOLKIT .'/soapclient/SforcePartnerClient.php'); 

/**
 * Reads SOAP incoming message from Salesforce/MAPS
 * @var incoming SOAP request
 */
$data = fopen('php://input','rb');

$headers = getallheaders();
$content_length = $headers['Content-Length'];
$buffer_length = 1000; // Do I need this buffer? 
$fread_length = $content_length + $buffer_length;

$content = fread($data,$fread_length);

/**
 * Parse values from soap string into DOM XML
 */
$dom = new DOMDocument();
$dom->loadXML($content);
$resultArray = parseNotification($dom);
$sObject = $resultArray["sObject"];

// Can remove this once I figure out the bug
$testing = false;

// Set $testing to true if you would like to see the incoming SOAP request from SF
if($testing) {
    // Make it look nice
    $dom->formatOutput = true;

    // Write message and values to a file
    $fh = fopen(LOG_FILE_PATH.'/'.LOG_FILE_NAME,'a');
    fwrite($fh,$dom->saveXML());
    $ret_val = fclose($fh);
}

/**
 * Checks if the SOAP request was parsed out,
 * the $sObject->ACK is set to a string value of true in
 * the parseNotification()
 * @var $sObject->ACK
 */
if($sObject->ACK == 'true') {
    respond('true');
} else {
    // This means something might be wrong
    mail(BAD_ACK_TO_EMAIL,BAD_ACK_EMAIL_SUBJECT,$content,BAD_ACK_EMAIL_HEADER_WITH_CC);
    respond('false');
}

if(WRITE_OUTPUT_TO_LOG_FILE) {
    // Clear variable
    $fields_string = "";

    /**
     * List common values of the SOAP request
     * @var $sObject
     */
    $fields_string .= "Organization Id: " . $sObject->OrganizationId . "\n";
    $fields_string .= "Action Id: " . $sObject->ActionId . "\n";
    //$fields_string .= "Session Id: " . $sObject->SessionId . "\n"; // Session Id is not being passed right now, don't need it
    $fields_string .= "Enterprise URL: " . $sObject->EnterpriseUrl . "\n";
    $fields_string .= "Partner URL: " . $sObject->PartnerUrl . "\n"; 

    /**
     * @todo: Still need to add the notification Id to an array or some sort
     */
    //$fields_string .= "Notification Id: " . $sObject->NotificationId . "\n"; 
    //$fields_string .= '<pre>' . print_r($sObject->NotificationId,true) . '</pre>';

    /**
     * now you have an array as $record and you can use the
     * data as you need to for updates or calls back to salesforce
     * whatever you need to do is here
     * @var $resultArray['MapsRecords']
     */
    foreach ($resultArray['MapsRecords'] as $record) {
        // Just prints the fields in the array
        $fields_string .= '<pre>' . print_r($record,true) . '</pre>';  
    }

    // Flag used to send ACK response
    $fields_string .= "\nACK Flag: " . $sObject->ACK;

    // $content_length
    $fields_string .= "\nContent Length (Outbound Message Size): " . $content_length;

    // Close Border to separate each request
    $fields_string .= "\n /*********************************************/ \n";

    // Write message and values to a file
    $fh = fopen(LOG_FILE_PATH.'/'.LOG_FILE_NAME,'a');
    fwrite($fh,$fields_string);
    $ret_val = fclose($fh); 
}

/**
 * Parse a Salesforce.com Outbound Message notification SOAP packet
 * into an array of notification parms and an sObject. 
 * @param   XML [$domDoc] SOAP request as XML
 * @return  object/array[ $result] typecast XML to object of arrays
 **/
function parseNotification($domDoc) {  
    // Parse Notification parameters into result array
    $result = array("OrganizationId" => "",
                    "ActionId" => "",
                    "SessionId" => "",
                    "EnterpriseUrl" => "",
                    "PartnerUrl" => "",
                    "sObject" => null,
                    "MapsRecords" => array());

    // Create sObject and fill fields provided in notification
    $sObjectNode = $domDoc->getElementsByTagName("sObject")->item(0);
    $sObjType = $sObjectNode->getAttribute("type");

    if(substr_count($sObjType,"sf:")) {
        $sObjType = substr($sObjType,3);
    }

    $result["sObject"] = new SObject($sObjType);
    $result["sObject"]->type = $sObjType;    
    $result["sObject"]->OrganizationId = $domDoc->getElementsByTagName("OrganizationId")->item(0)->textContent;
    $result["sObject"]->ActionId = $domDoc->getElementsByTagName("ActionId")->item(0)->textContent;
    $result["sObject"]->SessionId = $domDoc->getElementsByTagName("SessionId")->item(0)->textContent;
    $result["sObject"]->EnterpriseUrl = $domDoc->getElementsByTagName("EnterpriseUrl")->item(0)->textContent;
    $result["sObject"]->PartnerUrl = $domDoc->getElementsByTagName("PartnerUrl")->item(0)->textContent;

    /**
     * @todo: for multiple requests, need to add an array of Notification Id's
     *        might move this inside the loop or something
     *        might not need to do this as well
     */
    //$notificationId[] = $domDoc->getElementsByTagName("Id")->item(0)->textContent;
    //$result["sObject"]->NotificationId = $notificationId;

    $sObjectNodes = $domDoc->getElementsByTagNameNS('urn:sobject.BLAH.com','*');
    $result["sObject"]->fieldnames = array();
    $count = 0;
    $tempMapRecord = array();

    // Loop through each notification sObject
    foreach ($sObjectNodes as $node) {
        if ($node->localName == "Id") {
            if ($count > 0) {
                $result["MapsRecords"][] = $tempMapRecord;
                $tempMapRecord = array();                          
            }
            // @note: added the strip_tags() to strip out all HTML tags
            $tempMapRecord[$node->localName] = strip_tags($node->textContent);
        } else {
            // @note: added the strip_tags() to strip out all HTML tags
            $tempMapRecord[$node->localName] = strip_tags($node->textContent);
        }        
        $count++;

        // set flag for ACK
        $result["sObject"]->ACK = 'true';
    }
    // Finish last item
    $result["MapsRecords"][] = $tempMapRecord;

    return $result;
}

/**
 * ACK to SalesForce, True/False (Prints header)
 * @param object $tf
 * @return $ACK
 */
function respond($tf) {
    $ACK = <<<ACK
<?xml version = "1.0" encoding = "utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <notifications xmlns="http://BLAH.com/outbound">
            <Ack>$tf</Ack>
        </notifications>
    </soapenv:Body>
</soapenv:Envelope>
ACK;

    print trim($ACK); 
}

Salesforce的SOAP请求示例,将多个通知节点添加到一个较大的请求中.

Example SOAP Request from Salesforce, there would be multiple notification nodes added to a larger request.

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Body>
 <notifications xmlns="http://BLAH.com/outbound">
  <OrganizationId>BLAH</OrganizationId>
  <ActionId>BLAH</ActionId>
  <SessionId xsi:nil="true"/>
  <EnterpriseUrl>https://BLAH.com/</EnterpriseUrl>
  <PartnerUrl>https://BLAH.com/</PartnerUrl>
  <Notification>
   <Id>BLAH</Id>
   <sObject xmlns:sf="urn:sobject.BLAH.com" xsi:type="sf:Case">
    <sf:Id>BLAH</sf:Id>
    <sf:CaseNumber>BLAH</sf:CaseNumber>
    <sf:Case_Owner_ID_hidden__c>BLAH</sf:Case_Owner_ID_hidden__c>
    <sf:CreatedDate>2010-03-17T12:11:33.000Z</sf:CreatedDate>
    <sf:LastModifiedDate>2010-03-17T15:21:29.000Z</sf:LastModifiedDate>
    <sf:OwnerId>BLAH</sf:OwnerId>
    <sf:Status>BLAH</sf:Status>
   </sObject>
  </Notification>
 </notifications>
 </soapenv:Body>
</soapenv:Envelope>

推荐答案

PHP内存问题会提示

A PHP memory issue will say

PHP Fatal error: Out of memory (allocated 250871808)...

这很可能是来自Salesforce平台的错误终止或截断的数据-请尝试从SF调试第一个错误.

This is more likely to be incorrectly terminated or truncated data originating from the Salesforce platform - try debugging the first error from SF.

好的,看来您正在以一种过时的方式获取数据.尝试用stream_get_contents()替换fread(),并在获取输出后立即替换echo $content.

OK, it looks like you're grabbing data in an antiquated manner. Try replacing fread() with stream_get_contents(), and also echo $content straight after you get it to check the output.

这篇关于Salesforce/PHP-出站消息(SOAP)-内存限制问题? DOMDocument :: loadXML()标签问题中的数据过早结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 08:07