如何从网站集成JSON API

时间:2013-08-24 12:05:53

标签: json api

我被指派为我的客户网站集成API。此API由vision6.com.au提供。他们的网站上没有太多可用的信息。任何人都可以给我一个例子,它将联系vision6数据库并添加一个使用jquery和php开发的网站的联系人。

这是我尝试的方式

var newVal = {
    "id": 1,
    "method": "addUser",
    "params": [
        "APIKEY",
        "123456",
        {
            "username"     : "username_123",
            "password"     : "123456abc",
            "first_name"   : "First Name",
            "last_name"    : "Last Name",
            "email"        : "example@example.com",
            "mobile"       : "0412312312",
            "phone"        : "56565656",
            "fax"          : "57575757",
            "position"     : "Manager",
            "is_read_only" : true,
            "timezone"     : "Australia/Brisbane",
            "email_user"   : true,
            "is_confirmed" : true
        }
    ]
};

$.ajax({
    url: 'http://www.vision6.com.au/api/jsonrpcserver.php?version=3.0',  
    type: 'POST',
    beforeSend: function(){alert('sending');},
    data: newVal,
    //dataType: 'json',
    //data: JSON.stringify(newVal),
    //contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    //async: false,
    success: function(msg) { alert(msg);

    }

});

这是我从他们的文档中获取的 developers.vision6.com.au

5 个答案:

答案 0 :(得分:1)

对于那些想要使用Ruby进行集成的人,我创建了一个简单代码的要点:

require 'httparty'

url = 'http://www.vision6.com.au/api/jsonrpcserver.php?version=3.0'
api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

JSON.parse(HTTParty.post(url, headers: { 'Content-Type' => 'application/json' }, body: JSON.dump({
  'method' => 'getSessionInfo', 'params' => [api_key]
})))

JSON.parse(HTTParty.post(url, headers: { 'Content-Type' => 'application/json' }, body: JSON.dump({
  'method' => 'searchLists', 'params' => [api_key]
})))

list_id = 123456
JSON.parse(HTTParty.post(url, headers: { 'Content-Type' => 'application/json' }, body: JSON.dump({
  'method' => 'addContacts', 'params' => [api_key, list_id, [{ 'First Name' => 'Foo', 'Last Name' => 'Bar', 'Email' => 'foo@bar.com' }]]
})))

答案 1 :(得分:0)

我建议关注他们的文档

http://developers.vision6.com.au/3.0/guide/getting-started

文档提到了一个客户端库来访问他们的JSON-RPC。下载它,创建一个免费帐户并从他们的示例中生成一段PHP代码。它似乎也包含很多见解和例子。我只是引用了addbatch

的PHP代码
// setup vars
$list_id01     = 123;
$list_id02     = 456;
$message_id    = 111;
$batch_details = array
(array
  ('list_id'      => $list_id01,
  'type'         => 'list',       // all Contacts in List 123
  'time'         => 'send,        // populate Batch time of send
  )
array
  ('list_id'      => $list_id02,
  'type'         => 'contact',     // send to contact_list, next
  'contact_list' => array(2),array(17),array(18),
  'time'         => 'send,        // populate Batch time of send
));
$queue_id = $api->invokeMethod('addBatch', $message_id, $batch_details,
time() + (24*3600), true);

一旦您生成了执行授权请求和简单操作的代码片段,但不会按预期行事,您应该在SO上更新您的问题。即您应该发布代码段以及您获得的结果与您期望获得的结果。这可能真的有助于解决这个问题。

我还建议您直接联系该服务的支持。

PS

除非您能够以形式提出可能对其他用户有用的问题,否则很难找到您所寻求的答案。否则,您的问题可以被认定为太本地化

答案 2 :(得分:0)

您需要下载jsonRPCClient.php并将其包含在您的php文件中。从我与Vision6 API的交易,它不是非常全面,并且在你开始进入它之前没有多大意义。不幸的是,它们对你的开始并不是很有帮助。

我的代码可以帮助您入门

include 'includes/jsonRPCClient.php';    
$list_id = 1234;
$url = "http://www.vision6.com.au/api/jsonrpcserver.php?version=3.0";   
$apikey = 'YOURAPIKEYHERE'
$api = new JsonRpcClient($url);
    $contact = array();
    $contact[] = array(
         'First Name' => "John",
         'Email' => "sample@email.com"      
    );

$returnID = $api->addContacts($apikey, $list_id, $contact);

我发现的最重要的变化是

 $api->addContacts($apikey, $list_id, $contact); 

使用API​​的所有方法都遵循此结构

 $api ->APIMethod($apikey, $OtherRequiredFields/Arrays);    

答案 3 :(得分:0)

<?php
$data =  array(www.mtalkz.com)

‘dest’ => ‘0000000000’,

‘msg’=>’This is Test message’,

‘pass’=>’xyz’,

‘send’=>’ALERTS’,

‘uname’=>’ xyz ‘,

‘wapurl’=>’www.mtalkz.com‘

);

$curl = curl_init();

curl_setopt_array($curl, array(

CURLOPT_URL => “https://mtalkz.com/developer-tools/“,

CURLOPT_RETURNTRANSFER => true,

CURLOPT_ENCODING => “”,

CURLOPT_MAXREDIRS => 10,

CURLOPT_TIMEOUT => 30,

CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,

CURLOPT_CUSTOMREQUEST => “POST”,

CURLOPT_POSTFIELDS => $data,

CURLOPT_HTTPHEADER => array(

“cache-control: no-cache”,

“content-type: multipart/form-data”,

),

));

$response = curl_exec($curl);

$err = curl_error($curl);

curl_close($curl);

if ($err) {

echo “cURL Error #:” . $err;

} else {

echo $response;

}

答案 4 :(得分:0)

<?php
$data =  array(www.mtalkz.com)

‘dest’ => ‘0000000000’,

‘msg’=>’This is Test message’,

‘pass’=>’xyz’,

‘send’=>’ALERTS’,

‘uname’=>’ xyz ‘,

‘wapurl’=>’www.mtalkz.com‘

);

$curl = curl_init();

curl_setopt_array($curl, array(

CURLOPT_URL => “https://mtalkz.com/developer-tools/“,

CURLOPT_RETURNTRANSFER => true,

CURLOPT_ENCODING => “”,

CURLOPT_MAXREDIRS => 10,

CURLOPT_TIMEOUT => 30,

CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,

CURLOPT_CUSTOMREQUEST => “POST”,

CURLOPT_POSTFIELDS => $data,

CURLOPT_HTTPHEADER => array(

“cache-control: no-cache”,

“content-type: multipart/form-data”,

),

));

$response = curl_exec($curl);

$err = curl_error($curl);

curl_close($curl);

if ($err) {

echo “cURL Error #:” . $err;

} else {

echo $response;

}