Trying to make GET request to Mailchimp with CURL & OAuth? - php

Everything I look at online is showing how to use OAuth & Curl to make a POST request, but I want to make a get request to the Mailchimp API and I'm not getting any response it seems. I've already managed to go through the authentication and get the user's token & api URL. Now I'm just trying to pull in their lists. Here's the CURL code I've got currently:
$headers = array(
"Content-type: application/json",
"Authorization: OAuth ".$user['mct']
);
$curl = curl_init();
curl_setopt_array($curl,array(
CURLOPT_URL => "https://".$user['dc'].".api.mailchimp.com/3.0/lists",
CURLOPT_USERAGENT => "oauth2-draft-v10",
CURLOPT_HTTPHEADER => $headers,
CURLOPT_ENCODING => ''
));
$tresp = curl_exec($curl);
$lists = json_decode($tresp,true);
curl_close($curl);
Assuming $user['mct'] and $user['dc'] contain the proper values, any idea what I'm doing wrong here?

In case anyone ends up googling and finding this, my problem was that the user information I was getting from wordpress' get_results() function was an object and not an array. Took me forever to realize because for some reason this part of my plugin is preventing me from using print_r().
Now that it's actually going to the Mailchimp API I'm able to get and debug whatever error they're sending back.

Related

Sony Aibo 1000 endpoint with php?

I have an Ers 1000 with cloud plan from Sony and i am trying to setup ENDPOINT on the sony site in order to use some features. I am using a PHP server than run fine, but no way to get the endpoint validation on site website. I used a php page to get the "challenge" value, and i send it back to body using php "echo", this works fine with postman but no way on sony site. I noticed the way they post the challenge is harder that common post so i used
$source1 = json_decode(file_get_contents("php://input"));
And I finished with a page showing the body like a json :
$post = json_decode(file_get_contents('php://input'), true);
echo($post);
this give {"challenge":"1324111"}
on sony I enter this url : https://xxxserver.com/aibo/ where index.php have the code
any idea on how to implement this API endpoint? as sony error don't give details on what is wrong. thanks!
here are few points allowing me to do it :
header('Content-type: text/plain; charset=utf-8');
in an if loop :
http_response_code(200);
this line is important to read the data sent to body by sony :
$source1 = array();
$source1 = json_decode(file_get_contents("php://input"),true);
not gone deep with this one but it's the idea for the security key
$headers = apache_request_headers();
$token = $headers['X-Security-Token'];
about data sent back as json i use smary but the result will be like a blank page with only :
{ "challenge":"thesourcechallengekey"}
got from :
$source1['challenge'];
Note : i also noticed that .htaccess file could add too many restrictions, it was not origin of my issue but if i enable extra settings and controls to my php.ini it often block the Endpoint check, so try first removing all settings that are not mandatory in order to test the Endpoint.
About sending back the actions to aibo curl in PHP works fine, here is a small basic sample :
$post_url = 'https://public.api.aibo.com/v1/devices';
$data = '{}' ;
$postData = array();
header('Content-Type: application/json'); // Specify the type of data
// Setup cURL
$ch = curl_init($post_url);
curl_setopt_array($ch, array(
CURLOPT_SSL_VERIFYPEER => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Authorization: '.$bearer)
));
Where $bearer = "Bearer yourbearercodehere";
And you have to edit the first URL to add sony API endpoint action link

Convert unirest to curl

I want to use jsonWhois api but it makes the server request using Unirest, which looks like it's no longer maintained and I would prefer to use curl anyway.
How can I convert this code to use Curl instead??
$response = Unirest\Request::get("https://jsonwhois.com/api/v1/whois",
array(
"Accept" => "application/json",
"Authorization" => "Token token=<Api Key>"
),
array(
"domain" => "google.com"
)
);
$data = $response->body; // Parsed body
I've tried curl_setopt($ch, CURLOPT_URL, 'https://jsonwhois.com/api/v1/whois?token=123456&domain=google.com');, but it says HTTP Token: access denied.
You can actually use Postman app for something like this. I use it all the time and it works great.
You can simply enter the request into it:
And then simply click on "Code" (top right corner) and go to "PHP" -> "cURL". It will show you the exact code that you have to write to make that request using cURL:
I have no idea what jsonwhois is but, if everything is set up correctly, it should work.

How can I run this REST API call from PHP?

I'm currently building a project based on the Parse.com backend that includes uploading files.
Users can upload files and then access a list of these/download them, this all works fine.
However, I'm not sure how to implement the command to delete an upload. From the Parse.com forums as well as the Parse support document, the call is:
curl -X DELETE \
-H "X-Parse-Application-Id: <YOUR_APPLICATION_ID>" \
-H "X-Parse-Master-Key: <YOUR_MASTER_KEY>" \
https://api.parse.com/1/files/<FILE_NAME>
I've had a bit of a look online but the only curl commands I can find to execute commands is curl_setopt. I imagine the above needs to be converted, can anybody help with this or point me in the right direction?
So basically I need to be able to press a button on a website (through PHP) and have it run the above command.
Thanks in advance
According to given info you have to set custom request method 'DELETE' (by CURLOPT_CUSTOMREQUEST option) as well as custom headers (by CURLOPT_HTTPHEADER option).
So the code should look like this:
$options = array(
CURLOPT_NOBODY => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_HTTPHEADER => array(
'X-Parse-Application-Id: <YOUR_APPLICATION_ID>',
'X-Parse-Master-Key: <YOUR_MASTER_KEY',
),
CURLOPT_URL => 'https://api.parse.com/1/files/<FILE_NAME>',
);
$ch = curl_init();
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
echo $response;
If it's useful I made a simple class to handle the api calls request via Curl
https://github.com/niklongstone/php-api

Get size of queue in twilio from rest call in php without using library

My php/Yii application interacts with twilio. I know the sid of a queue. I want to get the current size of that queue. The thing is that I can't use the twilio php library (I don't want to get into the details). I'm using curl, but I keep getting 401 errors.
This is my code:
$curl = curl_init();
curl_setopt_array($curl,array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.twilio.com/2010-04-01/Accounts/AccountId/Queues/QUeueID.json',
CURLOPT_USERPWD => 'token:{AuthToken}'));
curl_exec($curl);
I don't what I'm doing wrong. I'm trying to follow the documentation:
http://www.twilio.com/docs/api/rest/queue
EDIT: I turned it into a get request, from a post request.
Also, I got a 401 unauthorized error, not a 411. Sorry about that. Typo.
SECOND EDIT:
So, I figured it out in a conversation with Kevin. Turns out that I needed:
CURLOPT_USERPWD => 'AccountID:Token'
If you are just trying to retrieve the size of a queue, you want to make a GET request, not a POST. It looks like you are setting CURLOPT_POST in your curl request.

How to send URL variables programmatically with response from my PHP endpoint

I have a basic API endpoint set up on my site, which a 3rd party site will use to verify certain info that is entered into a form by the user.
Here's the flow:
1. User is on 3rd party site.
2. User enters info into a form
3. Info is sent to my site's endpoint.
4. My site checks the information and returns a JSON object.
As you can see from #4, my API is currently set up to return a JSON object. After the info is checked, something like this happens:
header('content-type: application/json; charset=utf-8');
echo json_encode($response);
exit;
However, the 3rd party site is only set up to receive URL variables. Is there a way to pass back url variables programmatically? I realize I could theoretically send a new request, but it's not clear to me where that request should go (the internal workings of the 3rd party site aren't well documented), so I'd much prefer to send it as a response.
I hope this makes sense. Please comment if it doesn't. Thanks in advance!
You don't get to send GET/POST parameters in the response, but in the response body you can send whatever you want in whatever format you want - and they can use curl or file_get_content and parse it on their side (3rd party's website).
For example (on the 3rd party's website):
//setting a call to your server
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-Type: text/xml\r\n".
"Authorization: Basic ".base64_encode("$https_user:$https_password")."\r\n",
'content' => $body,
'timeout' => 60
)
);
$context = stream_context_create($opts);
$url = 'https://'.$https_server;
// Here they call your server
$result = file_get_contents($url, false, $context, -1, 40000);
// Here you'll parse the $result

Categories