通过API的Workflow Max添加作业

时间:2018-10-26 11:42:03

标签: php api

我正在尝试将工作添加到Workflow Max API。我似乎正在使用API​​,但是我不断收到错误消息:

Message not in expected format.  The following required element was missing - Job/ClientID

我确定已添加了客户端ID,但似乎有些问题。这是代码:

function post_job_to_workflow_max($job_data) {

    // configure our connection to the api
    $api_token = 'API_KEY';
    $acc_key = 'ACC_TOKEN';
    $url = 'https://api.workflowmax.com/job.api/add?apiKey=' . $api_token . '&accountKey=' . $acc_key;

    // Job data must match the format required by WorkflowMax
    // currently accepts XML data
    // see: https://www.workflowmax.com/api/job-methods#POST%20add
    $xml = new SimpleXMLElement("<Job></Job>");
    $xml->addChild('Name', $job_data[0]);
    $xml->addChild('Description', $job_data[1]);
    $xml->addChild('ClientID', 18754031);
    // $clientID = $xml->addChild('Client');
    // $clientID->addChild('ID', 18754031);
    // $clientID->addChild('Name', "TEST CLIENT");
    $xml->addChild('State', 'Planned');
    $xml->addChild('StartDate', $job_data[2]);
    $xml->addChild('DueDate', $job_data[3]);

    // print_r($xml);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml->asXML());
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: text/xml',
    'Content-Length: ' . strlen($xml->asXML()))
    );   
    $output = curl_exec($ch);
    curl_close($ch);

    $result = simplexml_load_string($output);
    print_r($result);

}

如果有使用WFM的经验的人,不妨听听您如何使用WFM。

谢谢

1 个答案:

答案 0 :(得分:0)

所以回答我自己的问题,我终于做到了。

我这样做的方法是从用来将客户端发布到WorkFlow Max的函数中返回客户端ID。查看代码:

1)发布客户端

function post_client_to_workflowmax($client_data) {

        // configure our connection to the api
        $api_token = 'YOUR_TOKEN';
        $acc_key = 'YOUR_KEY';
        $url = 'https://api.workflowmax.com/client.api/add?apiKey=' . $api_token . '&accountKey=' . $acc_key;

        // Client data must match the format required by WorkflowMax
        // currently accepts XML data
        // These indexes match up with how the data has been stored
        // see: https://www.workflowmax.com/api/client-methods#POST%20add

        $xml = new SimpleXMLElement("<Client></Client>");
        $xml->addChild('Name', htmlspecialchars($client_data[2]));
        $xml->addChild('Email', htmlspecialchars($client_data[9]));
        $xml->addChild('Phone', htmlspecialchars($client_data[10]));
        $xml->addChild('Address', htmlspecialchars($client_data[3]) . ' ' . htmlspecialchars($client_data[4]));
        $xml->addChild('City', htmlspecialchars($client_data[5]));
        $xml->addChild('Postcode', htmlspecialchars($client_data[7]));
        $xml->addChild('Country', htmlspecialchars($client_data[8]));
        $xml->addChild('IsProspect', 'No');
        $contacts = $xml->addChild('Contacts');
        $contact = $contacts->addChild('Contact');
        $name = $contact->addChild('Name', htmlspecialchars($client_data[0]) . ' ' . htmlspecialchars($client_data[1]));


        // POST request
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml->asXML());
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: text/xml',
    'Content-Length: ' . strlen($xml->asXML()))
        );
        $output = curl_exec($ch);
        curl_close($ch);

        // Create an array from the data that is sent back from the API
        $result = simplexml_load_string($output);
        $clientID = NULL;
        // here we get the ID created for this client and pass it into the variable $clientID
        foreach($result->Client as $k => $v) {
                $clientID = $v->ID;
        }
        return $clientID;
}

然后我们将该ID传递到我们的职位发布功能中,如下所示:

2)将工作发布到WFM

function post_job_to_workflow_max($job_data, $clientID) {

        // configure our connection to the api
        $api_token = 'YOUR_TOKEN';
        $acc_key = 'YOUR_KEY';
        $url = 'https://api.workflowmax.com/job.api/add?apiKey=' . $api_token . '&accountKey=' . $acc_key;

        // Job data must match the format required by WorkflowMax
        // currently accepts XML data
        // see: https://www.workflowmax.com/api/job-methods#POST%20add
        $xml = new SimpleXMLElement("<Job></Job>");
        $xml->addChild('ClientID', $clientID);
        $xml->addChild('Name', htmlspecialchars($job_data[0]));
        $xml->addChild('Description', htmlspecialchars($job_data[1]));
        $xml->addChild('State', 'Planned');
        $xml->addChild('StartDate', htmlspecialchars($job_data[2]));
        $xml->addChild('DueDate', htmlspecialchars($job_data[3]));

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml->asXML());
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: text/xml',
    'Content-Length: ' . strlen($xml->asXML()))
        );
        $output = curl_exec($ch);
        curl_close($ch);

        $result = simplexml_load_string($output);

}

然后调用这些函数看起来像这样:

$id = post_client_to_workflowmax($client);
post_job_to_workflow_max($job, $id);

其中$ client必须是数据数组。这适用于我的情况,但可能不适用于您的特定情况,因此您可能需要编辑字段等。

希望这对遇到相同问题的人有所帮助。不是最优雅的代码,但是它可以完成工作。

相关问题