Advertisement
Guest User

CF-Publish

a guest
Apr 16th, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.78 KB | None | 0 0
  1. <?php
  2.  
  3. // GET THE URL PARAMETERS
  4.  
  5. if (isset($_GET['loc'])) {
  6.  
  7. $loc = $_GET['loc'];
  8.  
  9. } else {
  10.     echo "You didn't specify a location!";
  11.     die;
  12. }
  13.  
  14. //TEMP SET TO THIS ID
  15. $loc='5uXiVGhhqoaiqIc6YO8Q60'; /// REMOVE ME
  16.  
  17. // GET THE CURRENT VERSION FOR THE ITEM SUPPLIED ABOVE
  18.  
  19. $curl = curl_init();
  20. curl_setopt_array($curl, array(
  21.     CURLOPT_RETURNTRANSFER => 1,
  22.     CURLOPT_URL => 'https://api.contentful.com/spaces/gzx8y8irje6a/entries/'.$loc.'?access_token=XXXX'
  23. ));
  24. $resp = curl_exec($curl);
  25. $resp = json_decode($resp,true);
  26. //echo '<pre>';
  27. //var_dump( $resp);
  28. //echo '</pre>';
  29. curl_close($curl);
  30.  
  31. // SET ALL THE VARIABLE DATA
  32.  
  33. $version = $resp['sys']['version'];
  34. $prevVersion = $resp['sys']['publishedVersion'];
  35.  
  36.  
  37.  
  38. $id = $resp['fields']['id']['en-US'];
  39. $name = $resp['fields']['name']['en-US'];
  40. $komm = $resp['fields']['komm']['en-US'];
  41. $votes = $resp['fields']['votes']['en-US'];
  42. //echo 'Old'.$votes;
  43.  
  44. // INCREMENT THE VOTE VALUE
  45.  
  46. $votes++;
  47. //echo 'New'.$votes;
  48. // SET THE VARIABLES TYPES
  49.  
  50. settype($id, "integer");
  51. settype($komm, "integer");
  52. settype($votes, "integer");
  53. settype($prevVersion, "integer");
  54.  
  55. // UPDATE THE ENTRY
  56.  
  57.  
  58. $service_url = 'https://api.contentful.com/spaces/gzx8y8irje6a/entries/'.$loc.'?access_token=XXXX';
  59. $ch = curl_init($service_url);
  60.  
  61. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  62. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  63. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/vnd.contentful.management.v1+json'));
  64. curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Contentful-Version: '.$version.''));
  65.  
  66.  
  67. $data2 = json_encode(array('fields' => array('id'=>array('en-US'=>$id),'name'=>array('en-US'=>$name),'komm'=>array('en-US'=>$komm),'votes'=>array('en-US'=>$votes))));
  68.  
  69.  
  70. curl_setopt($ch, CURLOPT_POSTFIELDS,$data2);
  71.  
  72. $response = curl_exec($ch);
  73. if ($response === false) {
  74.     $info = curl_getinfo($ch);
  75.     curl_close($ch);
  76.     die('error occurred during curl exec. info: ' . var_export($info));
  77. }
  78.  
  79. curl_close($ch);
  80. $decoded = json_decode($response);
  81. if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
  82.     die('error occurred: ' . $decoded->response->errormessage);
  83. }
  84.  
  85. // Publish the entry
  86.  
  87.  
  88. $puburl = 'https://api.contentful.com/spaces/gzx8y8irje6a/entries/'.$loc.'/published?access_token=XXXX';
  89. $ch = curl_init($puburl);
  90.  
  91. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  92. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  93. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/vnd.contentful.management.v1+json'));
  94. curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Contentful-Version: '.$prevVersion.''));
  95. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: 0'));
  96. //var_dump($ch);
  97. $what = curl_exec($ch);
  98. echo '<pre>';
  99. var_dump($what);
  100. echo '</pre>';
  101.  
  102. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement