1

i've an issue with my curl request and i think it's coming from wordpress, i'm using wordpress to push data using curl & an API

here is an example of curl method :

public function pushtest()
{
    $url = $this->server . ':' . $this->port . '/tartanpion/api/-default-/public/tartanpion/versions/1/nodes/' . $this->tempNode . '/children?tar_ticket=' . $this->ticket;
    $ch = curl_init();

    $wp_request_headers = array(
        'Authorization' => 'Basic YWRtaW46YWRtaW4=',
    );
    $args = new CurlFile('demo.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', '_al_demo.docx');

    $data = array(
        'overwrite' => 'true',
        'nodeType' => 'of:product',
        'method' => 'POST',
        'headers' => $wp_request_headers,
        'filedata' => $args,
    );
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    curl_setopt($ch, CURLOPT_VERBOSE, true);

    $verbose = fopen('php://temp', 'w+');
    curl_setopt($ch, CURLOPT_STDERR, $verbose);

    $result = curl_exec($ch);
    $json = json_decode($result);
    if ($result === false) {
        printf("cUrl error (#%d): %s<br>\n", curl_errno($ch),
            htmlspecialchars(curl_error($ch)));
    }

    rewind($verbose);
    $verboseLog = stream_get_contents($verbose);

    echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";

    $node['id'] = $json->entry->id;
    $node['aspects'] = $json->entry->aspectNames;

    $url = $this->server . ':' . $this->port . '/tartanpion/api/-default-/public/tartanpion/versions/1/nodes/' . $node['id'] . '/move?tar_ticket=' . $this->ticket;
    $ch = curl_init();

    $wp_request_headers = array(
        'Authorization' => 'Basic YWRtaW46YWRtaW4=',
    );

    $data = array(
        'targetParentId' => $this->pdfNode,
    );

    curl_setopt($ch, CURLOPT_URL, $url);
    $payload = json_encode($data);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $result = curl_exec($ch);

}
  • At first i was thinking that my method was simply execute 2 times but i've only one response from each request and i put error_log everywhere to be sure and that's not that ...

  • i'm using wordpress with docker container and my api is on another container. In my api log i can see Post 1 - Post 2 and again Post 1 - Post 2.

  • this is happening on request who take few seconds, just few get or little post and i don't have the issue.

  • If i take this method and use it outside of wordpress it's working well, i even try to launch this method using only php in my container and it's working too ...

so my question is what in wordpress can do this kind of bug ? i saw that the curl response has not the same structure in wordpress and outside so wordpress touch it but i don't see any parameter to set anywhere

thanks

Update : here is the hook i use :

add_action('updated_post_meta', 'onProductSave', 99, 4);
function onProductSave($meta_id, $post_id, $meta_key, $meta_value)
    {
        // Autosave, do nothing
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return;
        }

    // AJAX? Not used here
        if (defined('DOING_AJAX') && DOING_AJAX) {
            return;
        }

    // Check user permissions
        if (!current_user_can('edit_post', $post_id)) {
            return;
        }

    // Return if it's a post revision
        if (false !== wp_is_post_revision($post_id)) {
            return;
        }

        if ($meta_key == '_edit_lock') {
            if (get_post_type($post_id) == 'product') {
                $tartanpion = new tartPush($post_id);
            }
        }
    }
  • 1
    in the test outside wordpress, do you use the same cUrl version ? – mmm Dec 6 '17 at 11:44
  • Where and how is this function called? Please add the Hook or Filter. – janh Dec 6 '17 at 12:01
  • mmm : I use that to check curl version : php.net/manual/fr/function.curl-version.php is it correct ? with this i have 7.38.0 outside wordpress and inside – Aliiiiiiii Dec 6 '17 at 12:30
  • @jahn i put the hook in my first message – Aliiiiiiii Dec 6 '17 at 12:37
  • 1
    Can you try your function on another hook ? Then try in browser in private mode. I already have a issue like this with ajax that's was a chrome extension which load the page twice when updating a post from wp-admin. Hope it help ! – Elex Dec 6 '17 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.