简体   繁体   中英

How to make API request using PHP cURL?

My request requires a token to make API requests. Here is the cURL code they provide as example:

curl -v -L https://api.artsy.net/api/artists/andy-warhol -H 'X-Xapp-Token:myToken'

My code:

$postdata = array();
$postdata['client_id'] = 'myId';
$postdata['client_secret'] = 'myClient';
$postdata['xapp_token'] = 'myToken';
$cc =  curl_init();
curl_setopt($cc,CURLOPT_POST,1);
curl_setopt($cc,CURLOPT_RETURNTRANSFER,1);
curl_setopt($cc,CURLOPT_URL,"https://api.artsy.net/api/artists/andy-warhol");
curl_setopt($cc,CURLOPT_POSTFIELDS,$postdata);
$result = curl_exec($cc);
echo $result;

This should result in a JSON object that looks like:

{
  "id": "4d8b92b34eb68a1b2c0003f4",
  "slug": "andy-warhol",
  "created_at": "2010-08-23T14:15:30+00:00",
  "updated_at": "2017-08-25T17:25:51+00:00",
  "name": "Andy Warhol",
  "sortable_name": "Warhol Andy",
  "gender": "male",
  "birthday": "1928",
  "hometown": "Pittsburgh, Pennsylvania",
  "location": "New York, New York",
  "nationality": "American",
//...

However it results in this result:

{:type=>"other_error", :message=>"405 Not Allowed", "X-Frame-Options"=>"DENY", "X-Robots-Tag"=>"noindex", "Allow"=>"OPTIONS, GET, HEAD"}

I'm not too familiar with cURL so I've hit a wall with trying to translate the example code they provide. Any help would be deeply appreciated!

You're using POST

curl_setopt($cc,CURLOPT_POST,1);

and it looks like the API doesn't allow POST.

..."Allow"=>"OPTIONS, GET, HEAD"

You can check this Q&A for an example of how to use GET instead.

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