2016-09-09 19 views
2

私はphp関数からcurlを使用してサードパーティのAPIを呼び出しています。私はJSON形式(データ型は文字列)の応答を得ています。その応答をオブジェクトまたは配列に変換したい。私はを試しましたが、私はnullを得ています。ブラウザでレスポンスを表示し、その文字列レスポンスをPHP変数に貼り付けてコピーすると、値が得られます。だから私は問題が何かを把握することはできません。ここでJSONオブジェクトに文字列レスポンスを変換するには?

は私のコードです:

{"identifier":"id", "items":[{"apiResult":"INVALID", "apiResultMessage":"Invalid controls. the field 'resource' is mandatory the field 'type of item' is mandatory the field 'element id' is mandatory the field 'resource' is mandatory", "id":"", "idProject":"", "nameProject":"", "refType":"", "refId":"", "idResource":"", "nameResource":"", "idRole":"", "nameRole":"", "comment":"", "assignedWork":"", "realWork":"", "leftWork":"", "plannedWork":"", "rate":"", "realStartDate":"", "realEndDate":"", "plannedStartDate":"", "plannedEndDate":"", "dailyCost":"", "newDailyCost":"", "assignedCost":"", "realCost":"", "leftCost":"", "plannedCost":"", "idle":"", "billedWork":""}] } 

私も取得するために第二のparam "連想" を追加してみこの

$curl_response = str_replace("'", "\'", $curl_response); 
$curl_response = json_decode($curl_response); 
+0

JSON文字列が有効なようで、http://json-parser.com/a7826645 'json_last_error_msg()'を使用してJSONデコードのエラーメッセージを確認しようとしています –

+0

質問の一部をランダムに太字で表記しないでください。 –

+0

@Anant、私は問題の答えを示しました。 –

答えて

2

これを試してください: -

$curl_response = curl_exec($curl); 

function escapeJsonString($value) { 
    $escapers = array("\'"); 
    $replacements = array("\\/"); 
    $result = str_replace($escapers, $replacements, $value); 
    return $result; 
} 



$curl_response = escapeJsonString($curl_response); 

$curl_response = json_decode($curl_response,true); 

echo '<pre>';print_r($curl_response); 

echo $error = json_last_error(); 

リファレンスが取ら: - http://www.pontikis.net/tip/?id=5

あなたが有用であることが分かったリンクがある: - https://stackoverflow.com/a/20845642/4248328

+0

問題が解決しました。私は[ここ](http://stackoverflow.com/questions/17219916/json-decode-returns-json-error-syntax-but-online-formatter-says-the-json-is-ok)から解決策を得ました。 「最も一般的な部分」のコメントを参照してください。 –

0

を試してみました:ここ

$fullUrl = 'http://example.com/api/assignment/1'; 
$data = AesCtr::encrypt($data, 'My Key', 256); 
$curl = curl_init($fullUrl); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, ['data' => $data]); 
$curl_response = curl_exec($curl); 
$curl_response = json_decode($curl_response); 
echo '<pre>'; 
print_r($curl_response); 

は応答がありますjson_decode関数への配列:

$curl_response = json_decode($curl_response, true);

希望します。

関連する問題