Skip to content

Instantly share code, notes, and snippets.

@wordpress-lab
Last active July 15, 2019 19:17
Show Gist options
  • Save wordpress-lab/e1bc6f48d763e96a300c60d5cb2164cb to your computer and use it in GitHub Desktop.
Save wordpress-lab/e1bc6f48d763e96a300c60d5cb2164cb to your computer and use it in GitHub Desktop.
crud-php
<?php
$username = 'redeye007';
$password = 'redeye123';
$id= 1780;
// the standard end point for posts in an initialised Curl
$process = curl_init('http://wordpress-300829-921065.cloudwaysapps.com/wp-json/wp/v2/posts');
// create an array of data to use, this is basic - see other examples for more complex inserts
$data = array('slug' => 'rest_insert' , 'title' => 'new REST API insert update 258' , 'content' => ' newThe content of our stuff update post 2sdsd', 'excerpt' => 'smaller' );
$data_string = json_encode($data);
// create the options starting with basic authentication
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
//curl_setopt($process, CURLOPT_POST, 1);
// make sure we are POSTing change arg
curl_setopt($process, CURLOPT_CUSTOMREQUEST, "POST");
// this is the data to insert to create the post
curl_setopt($process, CURLOPT_POSTFIELDS, $data_string);
// allow us to use the returned data from the request
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
//curl_setopt($process, CURLOPT_SSL_VERIFYPEER, 0);
// we are sending json
curl_setopt($process, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
// process the request
$return = curl_exec($process);
curl_close($process);
var_dump($return);
<?php
$username = 'redeye007';
$password = 'redeye123';
$id= 1780;
// the standard end point for posts in an initialised Curl
$process = curl_init('http://wordpress-300829-921065.cloudwaysapps.com/wp-json/wp/v2/posts/'.$id);
// create an array of data to use, this is basic - see other examples for more complex inserts
$data = array('slug' => 'rest_insert' , 'title' => 'REST API insert update 258' , 'content' => 'The content of our stuff update post 2', 'excerpt' => 'smaller' );
$data_string = json_encode($data);
// create the options starting with basic authentication
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
//curl_setopt($process, CURLOPT_POST, 1);
// make sure we are POSTing change arg
curl_setopt($process, CURLOPT_CUSTOMREQUEST, "GET");
// this is the data to insert to create the post
curl_setopt($process, CURLOPT_POSTFIELDS, $data_string);
// allow us to use the returned data from the request
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
// we are sending json
curl_setopt($process, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
// process the request
$return = curl_exec($process);
curl_close($process);
var_dump($return);
<?php
$username = 'redeye007';
$password = 'redeye123';
$id= 1780;
// the standard end point for posts in an initialised Curl
$process = curl_init('http://wordpress-300829-921065.cloudwaysapps.com/wp-json/wp/v2/posts/'.$id);
// create an array of data to use, this is basic - see other examples for more complex inserts
$data = array('slug' => 'rest_insert' , 'title' => 'REST API insert update 258' , 'content' => 'The content of our stuff update post 2', 'excerpt' => 'smaller' );
$data_string = json_encode($data);
// create the options starting with basic authentication
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
//curl_setopt($process, CURLOPT_POST, 1);
// make sure we are POSTing change arg
curl_setopt($process, CURLOPT_CUSTOMREQUEST, "POST");
// this is the data to insert to create the post
curl_setopt($process, CURLOPT_POSTFIELDS, $data_string);
// allow us to use the returned data from the request
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
// we are sending json
curl_setopt($process, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
// process the request
$return = curl_exec($process);
curl_close($process);
var_dump($return);
<?php
$username = 'redeye007';
$password = 'redeye123';
$id= 1780;
// the standard end point for posts in an initialised Curl
$process = curl_init('http://wordpress-300829-921065.cloudwaysapps.com/wp-json/wp/v2/posts/'.$id);
// create an array of data to use, this is basic - see other examples for more complex inserts
$data = array('slug' => 'rest_insert' , 'title' => 'REST API insert update 258' , 'content' => 'The content of our stuff update post 2', 'excerpt' => 'smaller' );
$data_string = json_encode($data);
// create the options starting with basic authentication
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
//curl_setopt($process, CURLOPT_POST, 1);
// make sure we are POSTing change arg
curl_setopt($process, CURLOPT_CUSTOMREQUEST, "POST");
// this is the data to insert to create the post
curl_setopt($process, CURLOPT_POSTFIELDS, $data_string);
// allow us to use the returned data from the request
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
// we are sending json
curl_setopt($process, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
// process the request
$return = curl_exec($process);
curl_close($process);
var_dump($return);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment