简体   繁体   中英

php-curl request returns denied

I'm currently trying to fetch some data with a curl request from this link . Doing this returns the following:

"HTTP/2 403 server: AkamaiGHost mime-version: 1.0 content-type: text/html content-length: 293 expires: Sun, 11 Aug 2019 08:34:24 GMT date: Sun, 11 Aug 2019 08:34:24 GMT

Access Denied

You don't have permission to access " http://www.g2a.com/lucene/search/filter ?" on this server.

Reference #18.9d0c1502.1565512464.22e1446"

I know that curl works fine because it works with other requests, it's just this one that gets denied. Also, opening the link with a browser doesn't show the "Access Denied" error but actually returns the data I need.

This is the curl request copy-pasted from the code:

try{
    //  initiate curl (used to request data from other webpages)
    $ch = curl_init();
    // will return the response, if false it prints the response
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // set the url, eliminates headers from response
    curl_setopt($ch, CURLOPT_URL, $g2a);
    curl_setopt($ch, CURLOPT_HEADER, true); 
    // execute
    $result=curl_exec($ch);

    //if some error occurs
    if (!$result)
        throw new Exception(curl_error($ch), curl_errno($ch));

    // Closing
    curl_close($ch);
} catch(Exception $e) {
    trigger_error(sprintf('Curl failed with error #%d: %s', $e->getCode(), $e->getMessage()), E_USER_ERROR);
}
var_dump($result);
//converts json to associative array
$result=json_decode($result, true);

Any ideas on what could be the problem?

If you want to use SSL with CURL you should down a root certificate from: https://curl.haxx.se/docs/caextract.html

Just download the cacert.pm with the link on the top of the content.. and tell which certificate to use when connecting to SSL. This sets an approiate connection (an actually secure connection opposed to use ssl_verifyer to false...)

My qualified guess is that the server you're connecting to do probably not set any incoming requests as valid (through something called CORS (Access-Control-Allow-Origin)). If you want connect from www.yourdomain.com then they would have to set up that www.yourdomain.com is valid for incoming requests.

I've tested other domains that the code below works with, so you would have to talk to the owners of g2a.com to handle this issue (it's a server issue, not only a code issue)

<?php
$g2a = 'https://www.g2a.com';

//Tell cURL where our certificate bundle is located.

//an absolute path to your downloaded pem-file:
$certificate = "C:\wamp\www\stackoverflow\cacert.pem"; 

try{
    //  initiate curl (used to request data from other webpages)
    $ch = curl_init();
    // will return the response, if false it prints the response
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CAINFO, $certificate);
    curl_setopt($ch, CURLOPT_CAPATH, $certificate);

    // set the url, eliminates headers from response
    curl_setopt($ch, CURLOPT_URL, ($g2a) );
    curl_setopt($ch, CURLOPT_HEADER, true); 
    // execute
    $result=curl_exec($ch);

    //if some error occurs
    if (!$result)
        throw new Exception(curl_error($ch), curl_errno($ch));

    // Closing
    curl_close($ch);
    } catch(Exception $e) {
        trigger_error(sprintf('Curl failed with error #%d: %s', $e->getCode(), $e- 
        >getMessage()), E_USER_ERROR);
}
var_dump($result);

//converts json to associative array
$result=json_decode($result, true);

Access the HTTPS URL directly:

$g2a = "https://www.g2a.com/lucene/search/filter";

there is no auth.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM