How do I call mongodb rest api through curl in php? - php

Execuse me for my bad english.
I am new to mongolab.I want to know how to call mongolad rest api through curl in php.
I don't want to use mongodb driver.
thanks in advance.

Try this (replace myApiKey with your api key form MongoLab):
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.mongolab.com/api/1/databases?apiKey=myAPIKey");
curl_setopt($ch, CURLOPT_USERAGENT, "MongolabBot/1.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

Related

Same API returns different results

I need some advice. I need to get data from API to display it on my application. The API is working both through Postman or through the browser, but when I upload the same code to the server, my API response returns NULL.
Here is the response I am getting with POSTMAN:
And here is the code I am using:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.instagram.com/myexcellentuser/?__a=1');
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$file_data = json_decode(curl_exec($ch));
curl_close($ch);
var_dump($file_data);
And here is what this same code returns on the app hosted on a server:
I would be extremely grateful for the advice!
Thanks!

Clickbank API - confirming that order has been successful?

I am new to API's and cURL (sorry).
I am looking to use Clickbank's API to check if an order has been successful - https://api.clickbank.com/rest/1.3/orders2
I understand that what I have here is a basic curl request:
define('CLICKBANK_DEV_KEY','DEV-KEY');
define('CLICKBANK_API_KEY','API-KEY');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.clickbank.com/rest/1.3/orders/list");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json", "Authorization:".CLICKBANK_DEV_KEY.":".CLICKBANK_API_KEY));
$result = curl_exec($ch);
curl_close($ch);
print $result;
How would I use their API to check if an order is successful?
Any tips or resources are appreciated, thank-you.

D&B API version 2, REST Authentication using PHP

I want get Authentication Token of D&B API version 2 REST methodology
http://developer.dnb.com/docs/2.0/common/authentication-process
Sample Request - Get New Token
POST https://maxcvservices.dnb.com/rest/Authentication
x-dnb-user: P2000000FD4A9DE85D848229E03507C8
x-dnb-pwd: volcano
I have already tested these credentials on Chrome Extension (Advanced REST Client), they worked fine.
So now How can I achieve this using PHP code, Please help
Thanks
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://maxcvservices.dnb.com/rest/Authentication');
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('x-dnb-user: P2000000FD4A9DE85D848229E03507C8','x-dnb-pwd: volcano'));
$param_array = array();
$param_query_string = http_build_query($param_array);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param_query_string);
$response = curl_exec($ch);
I had a discusstion with DnB technical team and they sent me .NET example code, I learned code and applied same logic using PHP and it worked finally.

Can't parse any data from a web page using curl

I am using curl to extract some data of a website.First time it works perfectly , but now it not parse any data from the website.I think they are blocked my server ip.So I can use public proxy ip.But still it not working.My code is ,
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urls);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, '3127');
curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
curl_setopt($ch, CURLOPT_PROXY, '204.93.54.15');
$original_file = curl_exec($ch);
curl_close($ch);
I can't get user name and password of public proxies.Is it important to set public proxy's username and password?It don't show any errors.Any one please help me.
Here is a good answer to use proxy in curl
How to use CURL via a proxy?
Please check this. If you have any queries feel free to ask.
Thanks
Close your connection when you are done with it. That should do the trick.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urls);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, '3127');
curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
curl_setopt($ch, CURLOPT_PROXY, '204.93.54.15');
$original_file = curl_exec($ch);
print_r($original_file);
curl_close($ch);

how can I create a function that can post my parameters using curl

I developed a API for my website. API working 100% correctly. But I designed it to catch and execute functions from this kind of POST parameters$_POST['functionName']; $_POST['parameter']; Now, in request page i want to send those as $_POST, I managed to do deal with that. But my problem is, how can I create a function that can post my parameters for each functions in API using curl. Here is my code which send requests to my API.
$url = 'http://localhost/myapi/api.php';
$postfields['functionName'] = "myFunction";
$postfields['parameter'] = "1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$data = curl_exec($ch);
$results = json_decode($data, true);
curl_close($ch);

Categories