CodeIgniter Forums
Curl User Agent question about documentation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Curl User Agent question about documentation (/showthread.php?tid=81462)



Curl User Agent question about documentation - eelisland - 03-06-2022

Hi,

According to documentation Here the syntaxe to specify a user agent is

Code:
'user_agent' => 'WDQS-example PHP/'. PHP_VERSION,

This syntaxe with WikiDATA SPARQL query always result a response of 403

As soon as i use this syntaxe

Code:
'User-Agent' => 'WDQS-example PHP/'. PHP_VERSION,

I get a response 200 with results for my sparql request.

I have made hundreds succesfull request to WikiPEDIA with the syntaxe 'user_agent'.

Is it a syntaxe error from the documentation or it's on the WIKIDATA Sparql api side ?


This is the code i'm using to send request

PHP Code:
    /**
    * Curl SPARQL query
    */
    public function curlQuerystring $sparqlQueryint $delay )
    {
        // CodeIgniter Curl client
        $client = \Config\Services::curlrequest();

        // WikiData SPARQL query url
        $url 'https://query.wikidata.org/sparql';

        // Curl request
        $result $client->request'GET'$url, [
            'headers' => [
                'Accept'    => 'application/sparql-results+json',
                'User-Agent' => 'WDQS-example PHP/'PHP_VERSION,
            ],
            'query'  => [
                'query'      => $sparqlQuery
            
],
            // Sleep before request
            'delay'  => $delay,
        ]);

        // Response code
        $code $result->getStatusCode();

        if ($code === 200)
        {
            // Get body response as array
            $array json_decode($result->getBody(), true);

            return $array ?? false;
        }
        else
        {
            throw new \Exception("WikiData error status code: "$code);
        }
    



RE: Curl User Agent question about documentation - kenjis - 03-06-2022

Can't reproduce.
The following code works fine.

PHP Code:
        $result $client->request('GET'$url, [
            'user_agent' => 'WDQS-example PHP/' PHP_VERSION,
            'headers'    => [
                'Accept' => 'application/sparql-results+json',
            ],
            'query' => [
                'query' => $sparqlQuery,
            ],
            // Sleep before request
            'delay' => $delay,
        ]); 



RE: Curl User Agent question about documentation - eelisland - 03-07-2022

Thank you for your feedback Kenjis.

EDIT:

I realise you are using a slightly different syntaxe with the 'use_agent' that i did not realise on first read.

You syntaxe fix my issue, thanks for your help.

Best.