0

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!

Sam Sverko
  • 1,480
  • 4
  • 15
  • 32

1 Answers1

3

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.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • Yeah, it's probably expecting a `GET` call, and the values as a query string. – ceejayoz Aug 25 '17 at 18:48
  • @ceejayoz Ah! Thank you for pointing that out! I replaced `curl_setopt($cc,CURLOPT_POST,1);` with `curl_setopt($cc, CURLOPT_CUSTOMREQUEST, 'GET');` and it now returns: `{"_links":{"location":{"href":"https://api.artsy.net/api/artists/4d8b92b34eb68a1b2c0003f4"}}}`. Unfortunately, my new problem is, I'm trying to access the "href" target, but keep getting an error: `Recoverable fatal error: Object of class stdClass could not be converted to string` with `$result = curl_exec($cc); $myJson = json_decode($result); echo $myJson->_links;` Any help on this? Thank you! – Sam Sverko Aug 25 '17 at 18:59
  • @Sam That new error isn't really related to the original problem you asked about here, so it's probably better to ask a new question about it. However, I'll warn you it's likely to be closed as a duplicate of [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) so I'd recommend taking a look at that first. – Don't Panic Aug 25 '17 at 19:05
  • @Sam I'll tell you, it looks like the URL should be at `$myJson->_links->location->href`. But it looks like the API could return multiple links, so you probably want to iterate `$myJSON->_links` instead. It's a bit much to cover in the comments section. – Don't Panic Aug 25 '17 at 19:07