Hey there I am started to learn foursquare API, but I am stuck at getting an Access Token.
Here is a part from the code I found in SO.
// build url
$url = 'https://foursquare.com/oauth2/access_token';
$url .= '?client_id='.CLIENT_ID;
$url .= '&client_secret='.CLIENT_SECRET;
$url .= '&grant_type=authorization_code';
$url .= '&redirect_uri=**********/callback'; //change to your 4sq callback
$url .= '&code='.$code;
// call to https://foursquare.com/oauth2/access_token with $code
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
However this did not work, so I have tried to find error. First echoed $url and manually clicked on that link. It worked, foursquare has returned me an access token in json format. So the problem is in the curl part of the code.
Can you find my error? and more importantly can you suggest me some resources to study on curl?
EDIT:
I did a var_dump($result) and the output is 'boolean false'
The problem is http*s*, try adding these:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
If it is a POST a request then this is the proper way to do it :
$body.='client_id='.CLIENT_ID etc.
$c = curl_init ();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt ($c, CURLOPT_POST, true);
curl_setopt ($c, CURLOPT_POSTFIELDS, $body);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec ($c);
curl_close ($c);
Related
I'm new to PHP and am currently struggling to set this up. Could someone provide me an outline of how you use curl to post and retrieve the returning data?
I tried this out:
$url = 'http://localhost:8080/ds/stuff?maybe=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
But I keep getting the HTTP status 405 Error
(aka this):
Am I doing something wrong/what should I do? Or do I have to change something in my ds/stuff
You should set CURLOPT_POST to true. POST data goes to CURLOPT_POSTFIELDS.
$url = 'http://localhost:8080/ds/stuff?maybe=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
I have a problem to get the data using curl operation. Here i hide the token, If i use the url only in my browser then it returns the data but here its null.
<?php
$token = "TOKEN"; //the actual token hidden
$url = "https://crm.zoho.com/crm/private/xml/Leads/getRecords?authtoken=".$token."&scope=crmapi";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
echo $result; //does not return anything
?>
Where i do mistake please help me.
This is how you can try with CURLOPT_RETURNTRANSFER which is used to return the output and curl_errno() to track the errors :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/my_url.php" );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($ch);
if (curl_errno($ch)) {
print curl_error($ch);
}
curl_close($ch);
Helpful Links: curl_errno(), curl_error()
Try adding these lines:
<?php
$token = "TOKEN"; //the actual token hidden
$url = "https://crm.zoho.com/crm/private/xml/Leads/getRecords";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("authtoken"=>$token,"scope"=>"crmapi"));
$result = curl_exec($ch);
curl_close($ch);
echo $result; //does not return anything
?>
please see below article , not just turn off verify , you should update your php.ini with pem file
https://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/
Anyone running a recent Linux distribution is probably already OK, because they get the curl libraries bundled in through their package managers and recent curl libraries come with the latest CA root certificate bundle from Mozilla.org.
For a PHP installation that doesn’t come with this file, like the Windows PHP distribution, you need to download the CA root certificate bundle and tell PHP where to find it.
I want to get the response from a PHP post as a string. I'm using curl, based in some references I've found here.
This is my function:
function curlPost($url, $parameters){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
It really seems right, but when I run it, it just doesn't happen anything. I've tried hardcoding $url and $parameter, but it also didn't work.
Any ideas?
Thanks!
I have written the following function, which was code I used somewhere else and modified slightly to work as a function (using $url in function parameters):
function curl2str($url) {
$cURL = curl_init($url);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_CONNECTTIMEOUT, 5);
$data = curl_exec($cURL);
curl_close($cURL);
return $data;
}
I simply want a function to return a URL into a string, for a quick and easy API. The URL I am passing it is valid and works fine when I put it into a browser. I am calling it like so:
<?=curl2str("**valid URL here**");?>
For some reason it is just returning false. What am I doing wrong?
update
When I put this questions URL into as $url, I get a response. But when I use my custom URL, which works fine in the browser, and simply display's a list of files in the directory, I get bool:false.
update 2
It would seem that any domain works fine, apart from the one that I am trying to access. It just so happens that this is a root domain on the same server, I am running this script from a subdomain, but because of basedir_restrictions I cannot access a folder from the subdomain. So I wrote a little php to get the contents of the folder, and output it to the browser as a serialized array (JSON is not installed). But I cannot get a response from this root domain at all. It works fine in the browser, just not in cURL. And everything else works fine in cURL.
:(
Try this code into your function:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if(curl_errno($ch))
echo 'Curl error: '.curl_error($ch);
curl_close ($ch);
Note: curl_errno($ch); return error number>0 if any error occurs from cURL and use curl_error($ch); to see what is the error from cURL.
I use this function:
function curl($url, $cookie = false, $post = false, $header = false, $follow_location = false, $referer=false,$proxy=false)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $follow_location);
if ($cookie) {
curl_setopt ($ch, CURLOPT_COOKIE, $cookie);
}
if ($post) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
$response = curl_exec ($ch);
curl_close($ch);
return $response;
}
I'm having trouble retrieving data from the google api. When I run the code, it only returns a blank page and not a printout of the xml array. Here is the code:
$url="http://www.google.com/ig/api";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "?weather=london,england");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
echo "<pre>";
print_r($data);
I think that the problem is that you use POST method, and not the GET
Try like this
$url="http://www.google.com/ig/api?weather=london,england";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
Hope it helps :)
EDIT: And yes, you have to do some extra parsing to get the data from the XML string