Solved

Adding wordpress post to website remotely

Posted on 2014-04-17
1
328 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

789 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