简体   繁体   中英

Using CURL to return JSON from API in PHP

How can I GET the contents of this API using the given method of CURL?

curl -X GET --header 'Accept: application/json' 'https://api.rezdy.com/v1/products?limit=2&offset=0'

It should give a response of

{
"requestStatus": {
    "success": false,
    "error": {
      "errorCode": "4",
      "errorMessage": "Missing API Key"
    }
  }
}

All attempts at using render the page empty and other methods of extracting the data from the API such as JS have a CORS issue.

Try this PHP code :

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api.rezdy.com/v1/products?limit=2&offset=0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");


$headers = array();
$headers[] = "Accept: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

Output :

{  
    "requestStatus":{  
        "success":false,
        "error":{  
            "errorCode":"4",
            "errorMessage":"Missing API Key"
        }
    }
}

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