'Google Search console API > index inspect gives bool(false)

I'm trying to build a script to use the new Google Search Console API url inspection method : https://developers.google.com/webmaster-tools/v1/urlInspection.index/inspect

I'm fine getting the token with Oauth, but I'm getting a "bool(false)" answer when I post parameters to the API with cURL in php.

Of course, that doesn't helps me a lot, and since the API is quite new (last week), there's no tutorial online.

Here's the curl part :


$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
        "Accept: application/json",
        "Content-Type: application/json",
"Authorization : Bearer $token"
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$data = '{"inspectionUrl":"%%% URL TO CHECK %%%%","siteUrl":"%%%% RIGHT GSC PROPERTY %%%%"}';

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);```

If someone could tell me what's wrong, that would be great !

Thank you


Solution 1:[1]

try this:

    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://searchconsole.googleapis.com/v1/urlInspection/index:inspect',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS =>'{"inspectionUrl":"%%%","languageCode":"%%%","siteUrl":"%%%"}
        ',
        CURLOPT_HTTPHEADER => array(
            'Authorization: Bearer '.$bearer,
            'Content-Type: application/json'
        ),
    ));

    $response = curl_exec($curl);
    curl_close($curl);
    echo $response; 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bartosh Drummer