Php 如何获得API响应

Php 如何获得API响应,php,api,curl,Php,Api,Curl,我有以下回应: { "Contratos": [ { "IdCUPS": 0, "CodigoCUPS": "", "IdContrato": 0, "CodigoContrato": null, "IdCliente": 0, "IdDocumento": null, "IdEmpresa": null,

我有以下回应:

{
    "Contratos": [
        {
            "IdCUPS": 0,
            "CodigoCUPS": "",
            "IdContrato": 0,
            "CodigoContrato": null,
            "IdCliente": 0,
            "IdDocumento": null,
            "IdEmpresa": null,
            "Incidencias": [
                {
                    "Propiedad": "CodigoCUPS",
                    "Code": 0,
                    "CodeAlfa": "",
                    "Mensaje": "El cups ya esta asignado a contratos activos",
                    "IsAdvertencia": false,
                    "IsError": true,
                    "IsExcepcion": false,
                    "ExceptionTimestamp": "0001-01-01T00:00:00",
                    "ExceptionMessage": "",
                    "ExceptionStackTrace": "",
                    "InnerException": "",
                    "ImageSource": ""
                }
            ]
        }
    ]
}
如何访问“Mensaje”选项? 我有一段代码,其中我制作了curl_init及其选项:

$ch = curl_init($url.$contratoPotencial);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
我尝试使用以下代码:

echo "<br><br> Mensaje: ";
echo "<br>".$response->Contratos->Incidencias->Mensaje;
echo”

Mensaje:“; 回声“
”$response->Contratos->Incidencias->Mensaje;

但它不起作用

像下面那样进行json解码

$respData = json_decode($response, true);
然后打印

echo $respData['Contratos']['Incidencias']['Mensaje'];

您在json中混合使用了数组和对象:请尝试以下解决方案:

$response = '{
    "Contratos": [
        {
            "IdCUPS": 0,
            "CodigoCUPS": "",
            "IdContrato": 0,
            "CodigoContrato": null,
            "IdCliente": 0,
            "IdDocumento": null,
            "IdEmpresa": null,
            "Incidencias": [
                {
                    "Propiedad": "CodigoCUPS",
                    "Code": 0,
                    "CodeAlfa": "",
                    "Mensaje": "El cups ya esta asignado a contratos activos",
                    "IsAdvertencia": false,
                    "IsError": true,
                    "IsExcepcion": false,
                    "ExceptionTimestamp": "0001-01-01T00:00:00",
                    "ExceptionMessage": "",
                    "ExceptionStackTrace": "",
                    "InnerException": "",
                    "ImageSource": ""
                }
            ]
        }
    ]
}';

$responseData = json_decode($response);
echo "<br><br> Mensaje: ";
echo "<br>".$responseData->Contratos[0]->Incidencias[0]->Mensaje;

将完整的json编码到数组中,如下所示:

$responseData = json_decode($response, true);
echo "<br><br> Mensaje: ";
echo "<br>".$responseData['Contratos'][0]['Incidencias'][0]['Mensaje'];
$responseData=json\u decode($response,true);
回声“

门萨耶:”; echo“
”$responseData['Contratos'][0]['Incidencias'][0]['Mensaje'];

在这两种情况下,您都将获得所需的输出

为什么该代码有任何工作希望?阅读还要注意的是,
Incidencias
是一个常规数组,因此您需要引用元素0,而不是像它不在那里一样跳过它。尝试单步遍历它,看看您实际使用的是什么,
var\u dump($response)
等等。对象和数组混合在一起,使用它们的语法各不相同(您试图将每个子项用作对象)尝试
$data=json\u decode($response);
$responseData = json_decode($response, true);
echo "<br><br> Mensaje: ";
echo "<br>".$responseData['Contratos'][0]['Incidencias'][0]['Mensaje'];