Drupal 7 开发内部培训资料, 分头诗人

如何使用Services 3.0 创建一个未发布的内容类型节点?

赞成!
0
否决!

使用Services rest server ,在A网站创建一个新节点并推送到B网站,同时把这个推送到B网站上的节点设置为“未发表”状态。

function group_infomation_service_login() {
	$endpoint = 'http://a.ms.com/services/rest';
	$data = array(
    'username' => 'admin', 
    'password' => 'admin', 
	);
	$data = http_build_query($data, '', '&');
	$headers = array();
	$options = array(
  'headers' => array(
    'accept' => 'application/json',
		'content-type' => "application/x-www-form-urlencoded",
	),
  'method' => 'POST',
  'data' => $data
	);
  $response = drupal_http_request($endpoint . '/user/login', $options);
	$data = json_decode($response->data);
	// Check if login was successful
	if ($response->code == 200) {
		// print t('Logged in to Bamboo.') . '<p>';
		// Now recycle the login cookie we recieved in the first request
		$options['headers']['Cookie'] = $data->session_name . '=' . $data->sessid;
		return $options;
	}
	return false;
}


function group_infomation_test_create() {
	$endpoint = 'http://a.ms.com/services/rest';
	$options = group_infomation_service_login();
	if (!is_array($options)) {
		print "error<p>";
		return;
	}
	//create a node
	$node_data = array(
		"title"=>"D3",
		"type"=>"page",
		"status"=> FALSE, 
		"language" => "und",
	);
	//use json
	$options['headers']['content-type'] = "application/json";
	$options['method'] = 'POST';
	$options['data'] = json_encode($node_data);
	$request = drupal_http_request($endpoint . '/node', $options);
	if ($request->error) {
		print ("Error doing http request");
		print '<pre>' . var_dump($request) . '</pre>';
		//dpm($request);
	}
	else {
		print ("Success doing http request");
		print '<pre>' . var_dump(json_decode($request->data)) . '</pre>';
		dpm($request_data);
	}
}

下面是返回的错误提示:

 

Error doing http request
object(stdClass)[10]
  public 'request' => string 'POST /services/rest/node HTTP/1.0
accept: application/json
content-type: application/json
Cookie: SESS2d38980816803afe977df0f81e1e2cf8=FMvb715SyRv6A4PUspvPsSiSPIyL4pXTE-kVYRnxkVg
User-Agent: Drupal (+http://drupal.org/)
Host: a.ms.com
Content-Length: 69

{"title":"DHC Tool 03","type":"page","status":false,"language":"und"}' (length=334)
  public 'data' => string '' (length=0)
  public 'protocol' => string 'HTTP/1.0' (length=8)
  public 'status_message' => string 'Not found: Could not find the controller.' (length=41)
  public 'headers' =>
    array (size=10)
      'date' => string 'Mon, 15 Jul 2013 07:20:10 GMT' (length=29)
      'server' => string 'Apache/2.2.22 (Win32) PHP/5.3.13' (length=32)
      'x-powered-by' => string 'PHP/5.3.13' (length=10)
      'expires' => string 'Sun, 19 Nov 1978 05:00:00 GMT' (length=29)
      'last-modified' => string 'Mon, 15 Jul 2013 07:20:10 +0000' (length=31)
      'cache-control' => string 'no-cache, must-revalidate, post-check=0, pre-check=0' (length=52)
      'etag' => string '"1373872810"' (length=12)
      'content-length' => string '0' (length=1)
      'connection' => string 'close' (length=5)
      'content-type' => string 'text/html' (length=9)
  public 'code' => string '404' (length=3)
  public 'error' => string 'Not found: Could not find the controller.' (length=41)

 

这个是什么地方不对吗? 真心没有思路。。希望有经验的朋友可以帮助一下

2 个回答

赞成!
0
否决!

services 模块我也不太熟悉。你可以看看官方文档:Services: An API for remote applications

赞成!
0
否决!

问题已解决。在开启services模块的时候我同时也把UUID services模块开启了,导致了总是报"can't find controller"的错误。