So if I put this in my browser URL:
https://maps.googleapis.com/maps/api/place/search/xml?location=45.508867,-73.554242&radius=500&sensor=false&key=mykey
It works. But if I try the exact same call in PHP I get ACCESS DENIED:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://maps.googleapis.com/maps/api/place/search/xml");
curl_setopt($ch, CURLOPT_POSTFIELDS, "location=45.508867,-73.554242&radius=500&sensor=false&key=mykey");
$result = curl_exec($ch);
echo $result;
?>
I tried many different options and minor changes but nothing works and I can't figure out why.
If you're doing this for testing purposes, make sure SSL verification is not enabled, as it will prevent it may prevent the request form being executed.
I believe you're missing this curl setting:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$latitude = 27.7172;
$longitude = 85.3240;
$radius = 50000;
$name = "kupondole";
$url = "https://maps.googleapis.com/maps/api/place/search/json?name=".urlencode($name)."&location=".$latitude.",".$longitude."&radius=".$radius."&key=MY_KEY";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
Works perfectly fine.
Related
My curl PHP code:
$url = "";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
If $url is, for example, https://www.google.com then all is OK.
But if $url is, for example, https://www.twitch.tv I cannot get answer, just getting:
504 error
What can be wrong?
It is probably due to the redirection to the https site. Try using the url https://www.twitch.tv.
It could your ip is Blacklisted try to use proxy and set SSL_VERIFYPEER to false :
$url = "https://www.twitch.tv";
$proxy='xxx.xxx.xxx.xxx:xxxx';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_PROXY,$proxy);
curl_setopt($ch, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$answer = curl_exec($ch);
print $answer;
i want to use CURL with addon ip which is added on my server So i am using below script for simple CURL pass.
<?php
function get_html($url,$ip) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_INTERFACE, $ip);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$ip="173.214.xxx.198";
$l = get_html("http://www.checkip.com/",$ip);
echo $l;
?>
This script running fine on other server . Please give me solution of this problem
If it works fine on another server then there's nothing wrong in this script. Do you have curl installed on the server that's failing?
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 am working on core php.
I am requesting a URL by curl. This is working on my development server but same thing are not working on live server.
Below is my Code:
$url = "http://www.streamatemodels.com/login.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'submitted=1&g=&sausr='.urlencode('username').'&sapwd='.urlencode('password').'');
$result = curl_exec($ch);
$error = curl_errno($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
print_r($result);
Please help me if anyone have solution.
Update
I checked this with new username and password but same things happen. Its working on my development server but not working on live and local host.
The following line:
curl_setopt($ch, CURLOPT_POSTFIELDS, 'submitted=1&g=&sausr='.urlencode('username').'&sapwd='.urlencode('password').'');
should be changed to:
curl_setopt($ch, CURLOPT_POSTFIELDS, 'submitted=1&g=&sausr='.urlencode($username).'&sapwd='.urlencode($password).'');
If it still doesn't work try to create the query-string outside of curl_setopt:
$params = 'submitted=1&g=&sausr='.urlencode($username).'&sapwd='.urlencode($password).'';
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
Another advise: it's better practice to use http_build_query($params)
UPDATE
I had the time to check the code, and it works fine on my local host (meaning: check your username/password!):
i have written a small script to connect via cURL to my API, however, i need to know how i can limit incoming cURL connections to prevent spam.
How can this be done?
<?php
function shorten_url($urltoshorten) {
$url = 'http://nn.pe/api.php?url='.$urltoshorten;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
// what to post
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
?>
You can use: curl_set_opt(CURLOPT_MAXCONNECTS, 10); for limiting connections you made by curl. Or you can create a table and keep record of IPs requesting for connections.
Hope this helps.