Solved

Adding wordpress post to website remotely

Posted on 2014-04-17
1
320 Views
Last Modified: 2014-04-20
Hi

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);
    
       curl_close($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
0
Comment
Question by:andieje
1 Comment
 

Accepted Solution

by:
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
            die(curl_error($ch));
            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;
}
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now