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!

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah, it's probably expecting a GET call, and the values as a query string.
@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 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? so I'd recommend taking a look at that first.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.