I am trying to use this PHP script to get the shortened link from bit.ly API.. It works fine but my question is there any way to make this script more efficient or take some unnecessary parts out of it. Also my main question is that when I use:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
I have to use the trim function on $data but when I use:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
I don't have to do that.. Why is it causing a line break after the link when I use 1 instead of true?
<?php
function get_bitly_short_url($url, $format = 'txt')
{
$connectURL = 'http://api.j.mp/v3/shorten?login=(MY USERNAME)&apiKey=(MY API)&uri=' . urlencode($url) . '&format=' . $format;
return curl_get_result($connectURL);
}
function curl_get_result($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 trim($data);
}
$short_url = get_bitly_short_url('http://google.com');
?>
1 !== true in common case, but in your case, should be no difference,
double check all other things...
I make simple test for you:
<?php
$url = 'http://ziptasticapi.com/92530';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$data = curl_exec($ch);
curl_close($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$data1 = curl_exec($ch);
curl_close($ch);
var_dump($data);
echo "\n";
var_dump($data1);
Results:
string(52) "{"country":"US","state":"CA","city":"LAKE ELSINORE"}"
string(52) "{"country":"US","state":"CA","city":"LAKE ELSINORE"}"
So, bugs in php happens, but no bugs this time
No difference, check other parts of your system
Luck!
Related
I have many HTTPS-URLS where I have to find a Special Phrase, so I´m trying to use cURL in a foreach Loop, but it´s not working.
...
foreach($sites as $site) {
$URL = $site;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
curl_close($ch);
}
print substr($response, $start, $i);
...
If I use just a single HTTPS-URL I get the Phrase, but inside a foreach-loop it´s not working.
Can someone help me? (:
Please excuse my poor english..
this may help :) store result inside an array
$sites = ['https://stackoverflow.com','http://example.org'];
$result = [];
foreach ($sites as $site) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $site);
// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "PHP Curl");
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// execute the given URL, and return output
$result[] = curl_exec($ch);
curl_close($ch);
}
var_dump($result);
here is the link what Im trying with:
https://maps.googleapis.com/maps/api/distancematrix/json?origins=47.02842388681676|18.51792812347412&destinations=47.4735911253571|20.596618652343803&key=*******&language=en&units=metric
this works great from browsers. But gives FALSE from php:
$ch = curl_init();
$url = 'https://maps.googleapis.com/maps/api/distancematrix/json?origins='.urlencode($lat1).'|'.urlencode($long1).'&destinations='.urlencode($lat2).'|'.urlencode($long2).'&key='.urlencode('**********').'&language=en&units=metric';
echo $url;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$c = curl_exec($ch);
curl_close($ch);
//$c = json_decode($c);
echo '<pre>'; var_dump ($c);die;
You can try with file_get_contents:
$content = file_get_contents('https://maps.googleapis.com/maps/api/distancematrix/json?origins=47.02842388681676|18.51792812347412&destinations=47.4735911253571|20.596618652343803&key=*******&language=en&units=metric');
$content = json_decode($content);
print_r($content);
Below I have a piece of code to return some information through an API.
$page = 'http://api.duedil.com/sandbox/v2/company/03977902.json? fields=get_all&api_key=***********';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
This prints out the following results http://www.visitrack.co.uk/testdata.php
what i am trying to do is split the results in to individual variables.
Thanks
Something like this should do the trick:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // forces curl_exec() to return page as a string
$json = curl_exec($ch);
curl_close($ch);
$data = json_decode($json, TRUE); // parse json string to array
print_r($data);
I have a problem with cURL. It takes over 40 seconds to fetch a web page.
The function is:
function get_page(){
$url = get_url();
$timeout = 1000;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, $CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$return_data = curl_exec($ch);
print_r (curl_getinfo($ch));
curl_close($ch);
return $return_data;
}
Also, it seems that $return_data = curl_exec($ch) actually dumps the page.
I managed to solve this problem by changing the DNS to 8.8.8.8
I use file_get_contents function to get and show external links on my specific page.
In my local file everything is okay, but my server doesn't support the file_get_contents function, so I tried to use cURL with the below code:
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo file_get_contents_curl('http://google.com');
But it returns a blank page. What is wrong?
try this:
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
This should work
function curl_load($url){
curl_setopt($ch=curl_init(), CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$url = "http://www.google.com";
echo curl_load($url);
I encountered such a problem accessing Google Drive content via the direct link.
After calling file_get_contents returned 302 Moved temporarily
//Any google url. This example is fake for Google Drive direct link.
$url = "https://drive.google.com/uc?id=0BxQKKJYjuNElbFBNUlBndmVHHAj";
$html = file_get_contents($url);
echo $html; //print none because error 302.
With the code below it worked again:
//Any google url. This example is fake for Google Drive direct link.
$url = "https://drive.google.com/uc?id=0BxQKKJYjuNElbFBNUlBndmVHHAj";
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$html = curl_exec($ch);
curl_close($ch);
echo $html;
I tested it today, 03/19/2018
//You can try this . It should work fine.
function curl_tt($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo curl_tt("https://google.com");