I built an application on top of the foursquare API. It works perfectly on localhost but as soon as I upload it to a public website it stops working. I have checked to see if php is working by placing simple echos throughout my code and have had no issues. On my localhost I am able to echo information from the JSON the foursquare generates. When it is on the public server it echos nothing.
$urlgen = "https://api.foursquare.com/v2/venues/search?near={$city}&query={$query}&client_id={$client_id}&client_secret={$client_secret}&v=20141015";
$resultFour = fetchData($urlgen);
echo "$resultFour";
This works code returns JSON on localhost but not on the website.
Fetch Data:
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;
}
Check if your public server has curl installed (i assume that fethData use curl to connect to server). If you are sure that it is installed. For me works code:
private function fetchUrl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
$feedData = curl_exec($ch);
curl_close($ch);
return $feedData;
}
Related
I am writing a tool that accesses a set of external website pages. Here is my test code to see if I can retrieve the page:
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
/* curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); */
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_content = get_data('http://www.imdb.com/');
echo $returned_content;
The thing is when I pass Google (for example) as the URL, I get the Google homepage in my browser (sans images for obvious reasons), but when I pass the site I want to see, www.imdb.com, I get nothing. Why is this, and what can I do about it?
This really has me perplexed and other examples on Stackoverflow have not helped.
If I type into the browser :
https://api.bitfinex.com/v2/book/tXMRUSD/P0
I will get an long array of data which is correct.
With the following simple code :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.bitfinex.com/v2/book/tXMRUSD/P0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
I get :
bool(false)
The only difference is my browser fetches the data directly, while the PHP code is on a local website hosted using IIS on windows 10.
I've tried everything and just can't see where I'm going wrong. Any help will be most appreciated. (Ironically, all the authenticated and encrypted POST code works fine and I'm stuck on the easy stuff!)
Try This code it will work!!!!
<?php
$url= "https://api.bitfinex.com/v2/book/tXMRUSD/P0";
$ch = curl_init($url);
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);
print_r($result);
?>
i write following code to get html data from url and its working for https site like Facebook but not working for Instagram only.
Instagram returns the blank
<?php
$url = 'https://www.instagram.com';
$returned_content = get_data($url);
print_r($returned_content)
/* gets the data from a URL */
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
The Instagram will return only javascript, that can't be render by your browser because it uses dynamic path, so <script src='/path/file.js'> will try to get localhost/path/file.js instead of instagram.com/path/file.js and in this situation the localhost/path/file.js not will exist, so the page will be blank.
One solution is find a way to give the full HTML instead of the Javascript, in this case you can use the "User-Agent" to do this trick. You might know that JS not handle by the search-engine, so for this situation the Instagram (and many websites) give the page without JS that is supported by the bot.
So, add this:
curl_setopt($ch, CURLOPT_USERAGENT, "ABACHOBot");
The "ABACHOBot" is one Crawler. In this page you can found many others alternatives, like a "Baiduspider", "BecomeBot"...
You can use "generic" user-agent too, like "bot", "spider", "crawler" and probably will work too.
Here try this on
<?php
$url = 'https://www.instagram.com';
$returned_content = get_data($url);
print_r($returned_content);
/* gets the data from a URL */
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//Update.................
curl_setopt($ch, CURLOPT_USERAGENT, 'spider');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
//....................................................
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
You should pass
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false)
and other header info as above.
For more detail,Please see
http://stackoverflow.com/questions/4372710/php-curl-https
I have been trying to call my website on localhost:1055 and my curl_exec call hangs. When I invoke external URL, it is working fine and I am getting a response. I have tried to turn off my firewalls, I have gone through all articles on stack overflow, but I don't seem to get an answer to this problem. I have seen one post where someone had similar issue but there was no resolution for that. Can anyone help?
I am on OS X Al Capitan version 10.11.6 (15G1004) with PHP 5.6 and I am using internal web server of PHPstorm running on port 1055. This is my code:
<?php
function __singleRequestSending($data, $options = array()) {
// array of curl handles
$ch = curl_init();
// data to be returned
curl_setopt($ch, CURLOPT_URL, $data["url"]);
//curl_setopt($ch, CURLOPT_PORT, 1055);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, urldecode($data["data"]));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return $result;
}
$data = array(
'url' => "http://localhost:1055/info.php",
'data' => "abc"
);
$res = __singleRequestSending($data);
echo 123;
echo "<pre>";
var_dump($res);
print_r($res);
exit;
?>
I have a curl script that I have calling Rotten Tomatoes. Every time I run it, even in a for loop from 1 to 10, it runs infinitely. The only way to stop it is by restarting the server, the page continues to call the rotten tomatoes site until the server goes down. The curl script works for other APIs so it should work for this one. Here it is, any idea?:
For the $temp_movie, that gets its value and works properly.
$ch = curl_init();
$api_link = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=****&q=".$temp_movie."&page_limit=1";
echo $api_link."<br>";
curl_setopt($ch, CURLOPT_URL, $api_link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, '3');
$content = trim(curl_exec($ch));
curl_close($ch);
$rottentomatoes = json_decode($content, true);
I have no idea why this worked but like I said, the curl script worked for other APIs, so I tried copy and pasting the same curl code (again) and trying again. Some reason this works? Is there any difference that I'm just not seeing?:
$ch = curl_init();
$api_link = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=****&q=".$temp_movie."&page_limit=1";
curl_setopt($ch, CURLOPT_URL, $api_link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, '3');
$content = trim(curl_exec($ch));
curl_close($ch);
$rottentomatoes = json_decode($content, true);