I am trying to access some basic info from an XML based API. I'm new to this and am getting lost reading the guides on php.net and via google that seem to assume I already know the basics.
The input needs to be formatted as follows:
<query>
<auth_key>xxxxx</auth_key>
<command>get_account</command>
<account_id>11122</account_id>
</query>
The return will be in an XML format. I assume I need to use CURL to connect and send the input, but I'm lost - how should the PHP code look to do this?
::UPDATE::
okay, I'm still struggling and not making any progress. I've found a tutorial that has kinda lead me to the following code, but it's not doing anything and I can't figure out how/where I'm supposed to acutally send the XML data through to the url.
$URL = 'http://www.test.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
print_r($data);
I'm not getting this right and not sure what I'm supposed to be doing. Any help would be appreciated!
The API probably expects you to POST your request to the server, in which case an HTTP POST with cURL example should help. Check the HTTP code of server response (200 is good, others are probably bad) - and if it's good then parse the XML.
Most APIs have very good documentation and examples though. It's worth reading through them or Googling for other people in your shoes.
I ended up stumbling on the following link: http://www.phpmind.com/blog/2009/08/how-to-post-xml-using-curl/
From there I got the sample code working and manipulated it to suit my needs. I also ended up using http://php.net/manual/en/book.simplexml.php for the xml parsing and it seemed very straight forward.
Related
I am getting back into programming after being gone for 20 years. A lot has changed! lol...
I have a NodeJS server setup on Heroku for my mobile app. I am trying to add an event on my server that will add new user info to Pipedrive.com using their API.
They have only written their API examples in PHP. So I'm trying to translate PHP to Javascript, while also learning NodeJS, PHP, and understanding Pipedrive's API all at the same time.
They pointed me to Tonicdev which has been epically useful in getting my javascript syntax down. Since that uses live Pipedrive data when I add my token, I can do all my testing there too, before trying to upload and test on my actual Nodejs server. So that's handy!
But I'm still trying to get a grip on what's happening in their API code. This is my first time to implement an API by myself.
Here is the page I am trying to translate:
http://support.pipedrive.com/hc/en-us/articles/206679239-Creating-a-deal-using-the-REST-API-and-PHP-full-working-example-code-
I don't need the organization. Just the person and the deal.
In my create_person function, I found this php:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $person);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
My questions are:
What is curl_init and curl_setopt?
Do I need them in my Nodejs script file for this?
If not, what javascript do I use in their place?
Thanks for your patience. Learning a ton here!!!
curl is a PHP library that handles making requests.
In this case curl_init() is initialising a new request and curl_setopt is setting certain options.
You'll want to replace curl with an equivalent library for NodeJS. The request module is pretty good for this, although there's plenty of other options too.
I am working in a web application that was coded using procedural PHP. No framework, no MVC, no OOP. It is what it is. At this point in time, recoding to use some sort of framework is not feasible. To my benefit, it is very well organized, and so it's easy to work within. Anyway - they want to add on a Point of Sales system, and have landed on Kounta (kounta.com). Kounta has an API that is RESTful and returns JSON or XML.
I am absolutely brand new to writing applications that integrate with API's, and there are a lot of terms being slung around that I am not quite familiar with.
From my understanding, I need to authenticate myself using oAuth 2.0, and from there, can make server calls to pull data from their server.
The first piece of that is what I need help with.
I have my client ID and client secret. I am just not sure what to do with them, and how to pass them to their server via script in order to receive a token, so that I can then make those server calls.
The Kounta API Documentation can be found here (http://www.kounta.com/documentation/).
Any help that anybody could provide would be greatly appreciated. At this point, I am not sure where to even get started.
Here is the code that I am currently using. This code returns an error asking me to identify myself to Kounta.
<?php
$url = "https://api.kounta.com/v1/companies/5678/orders.json?created_gte=2013‑06‑01";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 4);
$json = curl_exec($ch);
if(!$json) {
echo curl_error($ch);
}
curl_close($ch);
print_r(json_decode($json));
?>
I'm not too familiar with Kounta, but I recently did a project which required me to fetch data from Instagram based on a users ID.
To do this, I used CURL. Instagram has a pretty open API and even some examples. So it wasn't too hard for me to figure it out.
See below for my example:
<?php
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$result = fetchData("https://api.instagram.com/v1/users/<user>/media/recent/?access_token=<access_token>&count=4");
$result = json_decode($result);
foreach ($result->data as $post) {
echo '<div class="col-sm-3">
<a href="'.$post->images->standard_resolution->url.'" data-lightbox="instagram" data-title="'.$post->caption->text.' '.implode(' #',$post->tags).'">
<img src="'.$post->images->thumbnail->url.'" alt="" /></a>
</div>';
}
?>
What this does is simply connect to Instagrams API with a user ID and access token inside the URL and returns a JSON Array.
My only purpose for showing you this is because this might be exactly what you will need to do going forward with Kounta, in some form or fashion. I have not reviewed their API or anything so I can't say for sure. However, I'm more than certain CURL will be your best bet.
I would advise you to fully read through their API once more and see what options they have or maybe even see if they offer any examples to help you get started.
EDIT: You mentioned they they return JSON and XML. This is good to know because that more than likely means you can use CURL to get data from their databases.
Let me know if you have any questions.
I am trying to do a simple GET request in PHP. In all examples I found it is done using http_get("http:/blahblah..."). I understand from http.so loaded but http_get undefined that there is some new API which doesn't have http_get. This API also doesn't have any readable documentation or examples. Could somebody please explain how I can do a GET request using the new http API? Or maybe there is some other easier way?
An easy way to use GET in PHP is to put it in a variable like so...
$myVariable = $_GET['HTTP_URI'];
Which will put the URL int he variable, for example http://example.com. Or you could do...
$myVariable = $_GET['id'];
Which will return 10 if you have the following URL...
http://example.com/index.php?id=10
Hopefully that's what your looking for after reading your question. Reply if it's not what your after and I'll have a rethink for you.
Okay, after spending over an hour trying to decypher the HTTP api documentation I solved this by using curl. HTTP api is overcomplicated and lacks proper documentation. This makes it completely unusable and frustrating, especially for someone who is new to PHP.
This does what I want:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.myurl.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
http://curl.haxx.se/libcurl/php/examples/getpageinvar.html
We would like to integrate Skydrive in our website, like we're currently doing with Google Drive but I'm lost in how to retrieve the root folders and files.
I've read these posts and docs:
Access SkyDrive using PHP and OAuth
Convert to PHP REST CURL POST
Skydrive API here: http://msdn.microsoft.com/en-us/library/live/
Curl doc on http://php.net - even if I'm still confused about that
And yet, I can't seem to get what I wanted.
I've already the access_token and it's correctly assigned, the scopes are also correct (I'm using wl.contacts_skydrive), so, I'm putting here part of the code I've so far so you can tell me what I'm missing, what I should add or if I'm taking a wrong turn.
//$token is correctly assigned
$url = 'GET https://apis.live.net/v5.0/me/skydrive/files?access_token='.$token;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
Thanks you for your time taken to help me.
You need to use CURLOPT_RETURNTRANSFER to return the transfer as a string.
Otherwise you will get true or false. Assuming you were getting true from curl_exec
Try adding
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
I know that using cURL i can see the destination URL, pointing cURL to URL having CURLOPT_FOLLOWLOCATION = true.
Example :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "www.example1.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
$info = curl_getinfo($ch); //Some information on the fetch
curl_close($ch);
$info will have the url of the final destination which can be www.example2.com.
I hope my above understanding is correct. Please let me know if not!.
My main question is, what all type of redirection cURL will be able to know?
Apache redirect, javascript redirects, form submition redirects, meta-refresh redirects!?
update
Thanks for your answeres #ceejayoz and #Josso. So is there a way by which I can follow all the redirect programatically through php?
cURL will not follow JS or meta tag redirects.
I know this answer is a little late, but I ran into a similar issue and needed more than just following the HTTP 301/302 status redirects. So I wrote a small library that will also follow rel=canonical and og:url meta tags.
https://github.com/mattwright/URLResolver.php
I found meta refresh tags to not provide much benefit, but they are used if no head or body html tag is returned.
As far as I know, it only follows HTTP Header redirects. (301 and 302).
curl is a multi-protocol library, which provides just a little HTTP support but not much more that will help in your case. You could manually scan for the meta refresh tag as workaround.
But a better idea was to check out PEAR HTTP_Request or the Zend_Http class, which more likely already provide something like this. Also phpQuery might be relevant, as it comes with its own http functions, but could easily ->find("meta[refresh]") if there's a need. Or look for a Mechanize-like browser class: Is there a PHP equivalent of Perl's WWW::Mechanize?
I just found this on the php site. It parses the response to find redirects and follows them. I don't think it gets every type of redirect, but it's pretty close
http://www.php.net/manual/en/ref.curl.php#93163
I'd copy it here but I don't want to plagiarize