Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Adding wordpress post to website remotely

Posted on 2014-04-17
Medium Priority
Last Modified: 2014-04-20

I'm trying to insert a wordpress post into a remote database. I am doing this via curl i php

This is the data that is being sent as the arguments for the post. This is called args. Each element is url encoded

   ( [comment_status] => open [ping_status] => [post_author] => 2 [post_content] => %3Ch2%3EInstalling+the+T8+to+T5+converter%3C%2Fh2%3E%0D%0A%3Cimg+class%3D%22alignright+size-full+wp-image-389%22+alt%3D%22installation%22+src%3D%22http%3A%2F%2Fsaveiteasy.dev3.co.uk%2Fwp-content%2Fuploads%2Finstallation.jpg%22+width%3D%22300%22+height%3D%22400%22+%2F%3E%3Ca+title%3D%22Energys+Group%22+href%3D%22http%3A%2F%2Fwww.energysgroup.com%2F%22+target%3D%22_blank%22%3EEnergys+Group+%3C%2Fa%3Eis+able+to+a+deliver+a+UK-wide+turnkey+installation+of+low-carbon+retrofit+technologies+including+its+Save+It+Easy+-+T8+to+T5+device.%0D%0A%0D%0AThe+Energys+warrantied+turnkey+service+includes+supply+and+installation+of+the+low-carbon+T5+tube+and+equipment+as+a+single+package.%0D%0A%0D%0ADesigned+to+make+things+trouble-free+for+clients%2C+it+combines+our+unique+experience+of+retrofit+work+with+sensible+pricing%2C+scheduling+flexibility+and+rigorous+quality+control.%0D%0A%0D%0AEnergys+Group+provides%3A%0D%0A%3Cul%3E%0D%0A%09%3Cli%3EInstallation%2C+commissioning%2C+warranties+and+on-going+maintenance+programmes.%3C%2Fli%3E%0D%0A%09%3Cli%3ETurnkey+solutions%2C+which+reduce+time+and+effort+by+combining+product+supply%2C+installation+and+project+management+in+a+single+highly-competitive+package.%3C%2Fli%3E%0D%0A%09%3Cli%3EHands-on+training+and+commissioning%2C+which+is+available+to+customers+who+prefer+to+use+their+own+staff%2Fcontractors.+This+helps+to+ensure+the+installation+fully+conforms+to+standards.%3C%2Fli%3E%0D%0A%3C%2Ful%3E%0D%0A%3Ch3%3EMaintenance+and+support%3C%2Fh3%3E%0D%0AEnergys+Group+is+highly+experienced+in+the+installation+and+maintenance+of+its+range+of+low-carbon+T8+to+T5+retrofit+solutions.%0D%0A%0D%0AOur+dedicated+Customer+Services+Division+provides+warranty+support+and+can+also+perform+ongoing+maintenance+on+our+range+of+energy-efficient+lighting+converters.%0D%0A%0D%0AOur+commitment+to+customers+does+not+end+with+the+installation+of+Save+It+Easy.+We+offer+a+full+range+of+Service+Contracts%2C+which+are+designed+to+give+the+correct+level+of+maintenance+and+additional+peace+of+mind+to+prevent+or+cover+unexpected+failures.%0D%0A%0D%0AOperators+and+owners+of+large+sites+can+sign+up+to+an+individually-negotiated+agreement+which+will+take+care+of+all+the+costs+associated+with+lighting+on+site.%0D%0A%3Ch3%3EHealth+and+Safety%3C%2Fh3%3E%0D%0AEnergys+Group%E2%80%99s+highly+trained+Installation+Team+conforms+to+the+stringent+requirements+of+CHAS+%28Construction+Health+and+Safety+Scheme%29.%0D%0A%0D%0AAs+an+experienced+contractor%2C+we+have+operated+under+many+customers%E2%80%99+health+and+safety+policies+and+received+excellent+customer+references+as+a+result. [post_excerpt] => [post_name] => installation [post_parent] => 0 [post_password] => [post_status] => publish [post_title] => Installation [post_type] => page [to_ping] => No [menu_order] => 0 [guid] => http%3A%2F%2Fsaveiteasy.dev3.co.uk%2F%3Fpage_id%3D174 )

Open in new window

I am tryingto get the id of the page that was just created and nothing is coming back

$new_id = get_data('http://wxxxxxxx/insert_page.php',$args);

$new_id is nothing    It used to work fine when i was doing the curl as a get not a post
This is the get data function. There are no error messages
   function get_data($url, $data) {
       $fields = '';
       foreach($data as $key => $value) { 
          $fields .= $key . '=' . $value . '&'; 
       rtrim($fields, '&');
       $post = curl_init();
       curl_setopt($post, CURLOPT_URL, $url);
       curl_setopt($post, CURLOPT_POST, count($data));
       curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
       curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
       $result = curl_exec($post);
        echo "Curl : " . print_r(curl_getinfo($post)) . "<br/>";  // get error info
    	echo "cURL error number:" .curl_errno($post) . "<br/>"; // print error info
    	echo "\n\ncURL error:" . curl_error($post) . "<br/>"; 

Open in new window

Is this anythig to do with the format of the array sent to the insert_post method. I didnt urlencode anything before.

Thanks in advance for any expertise you can share
Question by:andieje
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment

Accepted Solution

andieje earned 0 total points
ID: 40011379
This fixed it

function get_data($url, $data) {
//set POST variables

   foreach($data as $key => $value) {
      $fields .= $key . '=' . $value . '&';
   rtrim($fields, '&');
   $ch = curl_init();

         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_CONNETTIMEOUT, 999);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, count($data) );
      curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:') );
//   curl_setopt($ch, CURLOPT_POST, count($data));
//   curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

      $ret = curl_exec($ch);
      //echo "ret: " . $ret . '<br/><br/>';
      if (empty($ret)) {
            // some kind of an error happened
            curl_close($ch); // close cURL handler
      } else {
          $info = curl_getinfo($ch);
          curl_close($ch); // close cURL handler
          if (empty($info['http_code'])) {
            die("No HTTP code was returned");
                } else {
        // load the HTTP codes
              //$http_codes = parse_ini_file("path/to/the/ini/file/I/pasted/above");
        // echo results
              echo "The server responded." . $info['http_code'] . " " . "<br/><br/>";
      return $ret;

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question