私は最近、異なるサーバー間で複数のWebサイトから自分のデータベースにデータを挿入するためのカスタムAPIを書いています...しかし、それはいくつかの問題があり、私もリモートでAPIと通信するためのクラスを作成しました...そして、これがどこであるかもしれない...ここでjson_encodeとjson_decodeのカスタムphp apiがjson文字列を返さない
は私のAPIコードの抜粋のサンプル..です
if (function_exists($_GET['method'])) {
// function exists, so lets run it.
$_GET['method']();
} else {
// function does not exist so lets throw an error.
$data['respCode'] = "100";
$data['respMsg'] = "The method you have called does not exist. Please reformat your call.";
echo json_encode($data);
}
// methods
function newProspect() {
// lets first check for the lead in the database..
$sql = mysql_query("SELECT * FROM leads WHERE email = '".$_POST['email']."' LIMIT 1");
if (mysql_num_rows($sql) >= 1) {
// duplicate found, so lets just send the data..
$data = mysql_fetch_assoc($sql);
$data['respCode'] = "200";
$data['respMsg'] = "Duplicate Lead. Information echoed.";
echo json_encode($data);
} else {
// no duplicate found, so lets insert the data..
$sql = "INSERT INTO leads SET ";
foreach ($_POST as $key => $value) {
$sql .= $key." = '".$value."',";
}
$sql .= "register_date = '".$rdate."'";
$sql = mysql_query($sql);
if (!$sql) {
// could not insert the info into the database so lets throw an error.
$data['respCode'] = "102";
$data['respMsg'] = "Could not intert into database.";
$data['errorMsg'] = mysql_error();
echo json_encode($data); return json_encode($data);
} else {
// lead was inserted into the database just fine, so lets pass back the data.
$id = mysql_insert_id($sql);
$sql = mysql_query("SELECT * FROM leads WHERE `idleads` =".$id."");
$data = mysql_fetch_assoc($id);
$data['respCode'] = '200';
$data['respMsg'] = "Lead Entered into Database Successfully.";
echo json_encode($data);
}
}
}
可能性があり問題は..
<?php
class theApi {
public $apIdomain = 'http://www.domain.com/api/index.php'; // ie: https://www.mydomain.com/admin/
public $APData = array();
public $postUrl = '';
public function __construct() {
}
function method ($meth = 'newProspect') {
$this->postUrl = $this->apIdomain."?method=".$meth;
return $this;
}
function postData($pdata) {
foreach ($pdata as $key => $val) {
if ($key != 'submit') {
$this->APData[$key] = $val;
}
}
return $this;
}
function process() {
$this->APData['ipaddress'] = ip2long($_SERVER['REMOTE_ADDR']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->postUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->APData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,0);
curl_setopt($ch, CURLOPT_TIMEOUT,30);
$rawresponse = curl_exec($ch);
curl_close($ch);
return $rawresponse;
}
}
$ap = new theApi();
その後、私はちょうどそれをテストするために私の提出のアプリを持っている...私はhttp://www.differentdomain.com/api-test/index.php?method=newProspectに行くことによって、コードを実行すると
<?php
$_method = $_GET['method'];
require_once("api.php");
switch ($_method) {
case 'newProspect':
$data['name'] = "TestB";
$data['phone'] = "555-555-5555";
$data['email'] = "[email protected]";
$d = $ap->method('newProspect')->postData($data)->process();
break;
case 'updateProspect':
break;
case 'saveProject':
break;
case 'finishApplication':
break;
}
は、私は、ブラウザの出力で取得:{"idleads":"1886","classid":"1","ipaddress":"-949980134","register_date":"0000-00-00 00:00:00","name":"TestB","first_name":null,"last_name":null,"phone":"555-555-5555","phone2":null,"email":"[email protected]","age":null,"zip":null,"traffic_source":"1","affiliateid":null,"sversion":null,"purpose":null,"amount":null,"description":null,"respCode":"200","respMsg":"Duplicate Lead. Information echoed."}
は
だから、API自体は動作しています...しかし、私はどこでもjson_decodeを実行することはできませんし、CURLがAPIからすべてのデータを取得していないかのように見えます。これに関する助けは大いに評価されています。ここにそれを働かせるために。
ありがとう。変更に
新しい出力はカールする:
object(stdClass)#2 (20) { ["idleads"]=> string(4) "1886" ["classid"]=> string(1) "1" ["ipaddress"]=> string(10) "-949980134" ["register_date"]=> string(19) "0000-00-00 00:00:00" ["name"]=> string(5) "TestB" ["first_name"]=> NULL ["last_name"]=> NULL ["phone"]=> string(12) "555-555-5555" ["phone2"]=> NULL ["email"]=> string(33) "[email protected]" ["age"]=> NULL ["zip"]=> NULL ["traffic_source"]=> string(1) "1" ["affiliateid"]=> NULL ["sversion"]=> NULL ["purpose"]=> NULL ["amount"]=> NULL ["description"]=> NULL ["respCode"]=> string(3) "200" ["respMsg"]=> string(36) "Duplicate Lead. Information echoed." }
を言及されている...しかし、私は今、出力にjson_decodeを実行することはできません。.. – Johnny
@ Johnny投稿した出力は、既にオブジェクトにデコードされています。 '$ obj-> idleads'(1186) –
のようにアクセスすることができます。うん、私はそれを試してみました。どちらもうまくいきませんでした。しかし、私はそれを修正しました。 – Johnny