How do I use the Ning API to create something like this? - php

http://www.friendsorenemies.com/vip/blog/embedAll?pageSize=10
Just a list of the blog posts.
Here is the link to the API: http://developer.ning.com/docs/ningapi/1.0/index.html
I'm not much of a developer. Any help would be appreciated. Or if anybody is familiar with this stuff and wants to be hired, I'd be glad to pay some money.

Ok i will from what i see you are not familiar with APIs. You access the information using cURL there is a good page in the documentation that gives you examples.
Now for using curl it's fairly simple in php:
$url = 'https://external.ningapis.com/xn/rest/apiexample/1.0/Photo/recent?xn_pretty=true'
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Now you have all the information you need inside $response
Check the API documentation for the exact way to work with this API. also check curl on php.net

Related

How can I get and process a response from a PHP API

Ok I will try and keep this short. I'm making a request to a simple PHP API which can be done by AJAX through Javascript or JQuery, however I want to make the request using PHP. What is the best way to do this? Using the file_get_contents() function or CURL? If so how do I do it through CURL as the API requires I use GET not POST. Also the response I know is in XML, how do I then process the response once it comes back?
This question may have been asked many times, however when reviewing a lot of the questions and answers they are not specific to my needs on this one so please no answers with "Please see this link" as I can guarantee it won't answer the question in full as a lot of them are making requests from either Facebook API or another API that does not do what the API I am using does.
function getXML()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1/index.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "user:password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
$res = curl_exec($ch);
return $res;
}
$xml_data = getXML();
$doc = new DOMDocument();
$doc->loadXML($xml_data);
$wms = $doc->getElementsByTagName('WowzaMediaServer');
$wmstotalactive = $wms->item(0)->getElementsByTagName("ConnectionsCurrent")->item(0)->nodeValue;
$wmstotaloutbytes = $wms->item(0)->getElementsByTagName("MessagesOutBytesRate")->item(0)->nodeValue;
so you extracted from xml the value from ConnectionsCurrent key and MessagesOutBytesRate
.
If your link does not need to auth remove :
curl_setopt($ch, CURLOPT_USERPWD, "user:password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

PHP: To implement API or WebService to transfer simple data?

I'm a bit confused about API vs WebService. But anyway, which one will you follow to.. lets say to provide simple string or xml or json data return.
Currently i have implemented something like:
Server:
echo json_encode("hello, ".$_POST["q"]);
Client:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $server_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('q' => 'world!'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1);
$response = curl_exec($ch);
curl_close($ch);
Normally as the standard way, how do we even call/name it? Shall i name it API? Or WebService?
And in normal way (whenever we need to provide some data out of Server, like above) which one do we implement?

PHP Facebook graph API search cURL - no results

I've got a problem with the facebook graph API.
I'm trying to do a search through the posts on facebook filtered on a keyword, but I can't seem to fetch the results.
When I copy my url in a browser, it works fine.
I think it has something to do with my cURL parameters, i'm pretty new to this.
<?php
$keyword = $_POST['keyword'];
$graph_url = "https://graph.facebook.com/search?&type=post&locale=en_US&q=".$keyword;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $graph_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
print $result;
?>
Anyone knows what's wrong with this, or anyone who knows a different solution to fetch search results from facebook.
As you are trying to reach site through HTTPS, you should turn off SSL verification, or add certificate to verify with. Details here: http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
Quick fix:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
Or you can just change your url to:
http://graph.facebook.com/search?&type=post&locale=en_US&q=

I have seen many different ways to use CURL in PHP. Which is the "right" one?

I have read that CURL is way too fast than File Get Contents and less memory consuming. So, I will go with CURL. I read some articles to find info about it and how to use it properly and efficiently. The problem is that I found many ways of using CURL, I posted 3 variations below.
My question is which one is the best to use?
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);
$content = curl_exec($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$cache = curl_exec($ch);
curl_close($ch);
$ch = curl_init("");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close ($ch);
It all depends on what options you want. Most of cURL's options will have some default, which will be set if you do not manually set an option.
For example: CURLOPT_RETURNTRANSFER defaults to FALSE (so setting it to FALSE in your code would be redundant).
Passing a parameter to curl_int is just a shortcut for CURLOPT_URL. The other options all depend on what you want to do.
Here is a list of all the cURL options: http://php.net/manual/en/function.curl-setopt.php
Pick the options you want, exclude those you don't. There's no "right" way, just the way that works the way you want.
Its like asking,
I have see many ways people code PHP, but which one is right?
There is no correct answer for such question, as everybody is going to have their own opinion about every way.
So, I suggest you read the PHP.net's manual on CURL and choose the correct method based on your coding style, requirement and construct.
1 is SSL (https:// or ftps://)
2 I think is a plain old ftp:// or http:// url
3 I am not sure what it is. looks like generic URL stuff without SSL.
did you check the notes in the example and read the documentation to see what exactly the functions do or the code does? you can feed curl_init a URL string.
get the .chm documentation with notes. it will help a lot and speed up your research.
looks like you need to have to put in some code that does this:
<?php
function getfile($url) {
$result="";
$protocol=substr($url,0,strstr($url,":"))
if ("https"==$protocol || "ftps"==$protocol) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);
$content = curl_exec($ch);
} else {
//it's http or ftp
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close ($ch);
}
return $result;
}
?>

REST-ful connection utilizing cURL header issues

I've spent hours troubleshooting this utilizing PHP documentation, the API documentation, as well as other posts on stackoverflow, and am finally asking for help.
I am attempting to write an interface utilizing the new pbSmartConnections API: API Documentation
I have been having challenges with both fsockopen and cURL, however I seem to be able to get farther in the process utilizing cURL, so that's what I'm presenting here. Here's the challenge:
Per my understanding of the documentation, I should be passing the ApiKey as part of the header. When I do this, regardless of the different ways I've attempted to structure the code, I ALWAYS receive the following response:
{
"ErrorCode": 10,
"Message": "Unauthorized"
}
I'm hoping a fellow SO member can see something in my code below (please offer any criticisms and/or suggestions, too!):
(NOTE: The API key below IS valid. It's connected to an account with nothing of value in it, so feel free to use it in your testing)
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://rest.pbsyscontrol.com/v1/Ping");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type"=>"application/json", "Accept"=>"application/json", "ApiKey"=>"41460b3f-8f35-4878-b78d-49ca7f29c071"));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
?>
In case you are wondering, while I would like this to work as part of the header, I HAVE tried passing it as a part of the URL as well:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://rest.pbsyscontrol.com/v1/Ping?ApiKey=41460b3f-8f35-4878-b78d-49ca7f29c071");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type"=>"application/json", "Accept"=>"application/json", "ApiKey"=>"41460b3f-8f35-4878-b78d-49ca7f29c071"));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
?>
The PHP documentation says:
An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')
You thus want to use the following line instead of the original:
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Accept: application/json", "ApiKey: 41460b3f-8f35-4878-b78d-49ca7f29c071"));
This does not however, solve the problem that the ApiKey probably isn't valid.
I finally heard back from support, and what they indicated was that I was using the incorrect url (although at this time, it IS the url in their API Documentation)
The URL in the API documentation was their STAGING, not PRODUCTION. Amazing what switching the URL to the correct one that they sent in their reply - rest.api.pbsmartconnections.com does for the connection. That one change and everything began working properly.
THANK YOU all who took a look, and to #mvdnes for the recommendations on how to set headers.

Categories