【问题标题】:Using the Google Search Console API to test if a site is mobile friendly is failing with 400 errors使用 Google Search Console API 测试网站是否适合移动设备使用失败并出现 400 错误
【发布时间】:2018-11-01 13:10:43
【问题描述】:

我想检查一个网站是否“移动友好”,使用谷歌 api 访问他们自己的测试服务。

我通过 PHP 使用 curl 与 https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run 端点对话。参考谷歌 api 文档here

但是,我的代码返回的数据始终是 400 错误页面。

我所知道的是我的 API 密钥是有效的。如果我通过 linux shell 中的 curl 实用程序运行命令,它可以工作(参考谷歌文档中给出的示例)。

有人见过这个吗?这一定与我的 php 代码以及我在其中实现 curl 的方式有关...

$ch = curl_init();
$curlConfig = array(
    CURLOPT_URL            => "https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run?key=xxxx",
    CURLOPT_POST           => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER     => array('Content-Type:application/json'),
    CURLOPT_POSTFIELDS     => json_encode(array(
        'url' => 'https://www.test.com'
    ))
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
echo $result;

【问题讨论】:

    标签: google-api


    【解决方案1】:

    所以在 SO 的 outside 的帮助下,我得到了一个答案,基本上是设置以稍微不同的方式重写代码。我无法弄清楚究竟是什么症结所在,但如果格式如下,它就可以工作......

    $data="http://www.yourdomain.com/";
    $url="https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run?key=xxxxx";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    $payload = json_encode( array( "url"=> $data ) );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
    curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $api_content = curl_exec ($ch);
    curl_close ($ch);
    
    $api_result = json_decode($api_content);
    
    var_dump($api_result);
    

    【讨论】:

      猜你喜欢
      • 2020-01-29
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      • 2019-10-28
      相关资源
      最近更新 更多