WordPress.org

Ready to get started?Download WordPress

Forums

JSON API
Weird error on using JSON API (1 post)

  1. wprocks
    Member
    Posted 2 years ago #

    Hi good day,

    I'm using a fork from https://github.com/Achillefs/wp-json-api so that i can authenticate users from different domains when creating posts.

    I was trying to create a post via the plugin and I have a weird issue:

    Using a script in Python :

    import urllib2
    import json
    enter = urllib2.urlopen('http://localhost/public_html/website/website/?json=get_nonce&dev=1&controller=posts&method=create_post').read()
    enter_json = json.loads(enter)
    a = enter_json['nonce']
    
    u = urllib2.urlopen('http://localhost/public_html/website/website/?json=posts/create_post&dev=1&nonce='+a+'&categories=competitions&title=hello%20world&content=%3Cp%3Eyou%20suck%20this%20is%20competition%3C/p%3E&author=admin&user_password=123456&status=publish')
    u_json = json.loads(u.read())
    
    print u_json

    The API works perfectly.

    However ,when i perform the same logic in PHP:

    <?php 
    
    		function get_data($url)
    		{
    		  $ch = curl_init();
    		  $timeout = 5;
    		  curl_setopt($ch,CURLOPT_URL,$url);
    		  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    		  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    		  $data = curl_exec($ch);
    		  curl_close($ch);
    		  echo "now in get_data";
    		  echo "<br>";
    		  echo $data;
    		  return $data;
    		}
    
    		$root_url = "http://localhost/public_html/website/website/";
    		$nonce_url = $root_url."?json=get_nonce&dev=1&controller=posts&method=create_post";
    		$json = get_data($nonce_url,0,null,null);
    		$json_data =  json_decode($json, TRUE);
    		//echo $json_data;
    
    		// check that user is logged into WP
    		$nonce = $json_data['nonce']; // need to have an exception handler in case this fails
    
    		echo $nonce;
    
    		//echo "this is the last part of the url i need \n";
    		//echo $end; //  this is what i need for the URL -> save into DB
    		echo '\n';
    		// than show message -> data is saved into SQL by building a POST using
    		// JSON API
    		$wp_json_api_call="?json=posts/create_post&dev=1&nonce=".$nonce;
    		$wp_json_api_call_tags="&tags=Photos for Competition";
    		$wp_json_api_call_title="&title= hahah";
    		//$wp_json_api_call_content="&content=<img src='".$fb_photo_picture."' />"; // the img src thing
    		$wp_json_api_call_content="&content=hahahahah contnet";
    		$wp_json_api_call_status="&status=publish";
    		$post_user_name = '&author=admin';
    		$post_user_password = '&user_password=123456';
    		$post_json_api = $root_url.$wp_json_api_call.$wp_json_api_call_tags.$wp_json_api_call_title.$wp_json_api_call_content.$wp_json_api_call_status.$post_user_name.$post_user_password;
    		echo $post_json_api;
    
    		$json_api_response = get_data($post_json_api);
    		//$json_api_response = file_get_contents($post_json_api);
    
    		echo "---------------- \n";
    		//echo $json_api_response;
    ?>

    The JSON-API returns an error message : "You need to login with a user capable of creating posts."

    I was wondering if anyone have come across such errors ? Cos this is really weird ?

    How do i fix this ?

    I want to create posts from different domains to a wordpress blog. And I hope to do it in PHP.

    Thanks for your time. Thanks!

    http://wordpress.org/extend/plugins/json-api/

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic