integrating a function - function

this is the follow-up to my question about getting a div's content. the second function is the one im having a hard time with. im pretty sure i can call a function from another one, but im not sure about placing them into oneanother like i did here. its obviously a silly attempt to make the code work, since it gives me an error:
Blackline Frostbyte : in stock. : $139.99
Fatal error: Cannot redeclare get_string_between() (previously declared in /home/rambazar/public_html/cron.php:69) in /home/rambazar/public_html/cron.php on line 69
as i see this, the code is partially ok, because it gets a products stock info and the price tag, but the code stops and i cant figure out where get_string_between is redeclared, as it is only called. please help me sorting this out, thanks!
<?php
set_time_limit(1800);
include("admin/include/db.php");
error_reporting(E_ALL);
$res=mysql_query("select * from products");
while($row=mysql_fetch_array($res))
{
$availability=getavailability($row['newegg_productid']);
$price=getprice($row['newegg_productid']);
echo $row['productname']." : ".$availability." : ".$price."<br />";
}
function getavailability($itemId)
{
$url=trim("http://www.newegg.com/Product/Product.aspx?Item=".$itemId);
$ch = curl_init();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$content = curl_exec ($ch);
curl_close ($ch);
$content=strtolower($content);
$buff=$content;
$isAvailable=false;
$pos1=strpos($content,'<p class="note">')+16;
if($pos1==16)return "";
$pos2=strpos($content,'</p>',$pos1);
$availability= trim(substr($content,$pos1,($pos2-$pos1)));
return strip_tags($availability);
}
function getprice($itemId)
{
function get_string_between($string, $start, $end)
{
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0)
return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
$data = file_get_contents("http://www.newegg.com/Product/Product.aspx?Item=".$itemId);
$pricediv = get_string_between($data, '<div class="current" id="singleFinalPrice"><span class="label">Now:', '</div');
$price = strip_tags($pricediv);
return $price;
}
?>

Take out the get_string_between() out of the getprice() function and you should be good to go:
function get_string_between($string, $start, $end)
{
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0)
return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
function getprice($itemId)
{
$data = file_get_contents("http://www.newegg.com/Product/Product.aspx?Item=".$itemId);
$pricediv = get_string_between($data, '<div class="current" id="singleFinalPrice"><span class="label">Now:', '</div');
$price = strip_tags($pricediv);
return $price;
}

Related

How to PHP Convertapi HTML to PDF without physcal HTML file

i wish to convert an html document, which is stored as a string ($html) into PDF using convertAPI via CURL (ie no physical file)
i don't understand how I need to post the $html to the API, i was looking at the example on the convertapi webpage, but i don't seem to be able to make sense of it.
example pasted below.
$html = '<hmtl file contents>' ;
$parameters = array(
'Secret' => 'X?X?X?X?X?X?X',
);
function convert_api($src_format, $dst_format, $files, $parameters) {
$parameters = array_change_key_case($parameters);
$auth_param = array_key_exists('secret', $parameters) ? 'secret='.$parameters['secret'] : 'token='.$parameters['token'];
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER , false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_URL, "https://v2.convertapi.com/{$src_format}/to/{$dst_format}?{$auth_param}");
if (is_array($files)) {
foreach ($files as $index=>$file) {
$parameters["files[$index]"] = file_exists($file) ? new CurlFile($file) : $file;
}
} else {
$parameters['file'] = file_exists($files) ? new CurlFile($files) : $files;
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);
$response = curl_exec($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$error = curl_error($curl);
curl_close($curl);
if ($response && $httpcode >= 200 && $httpcode <= 299) {
return json_decode($response);
} else {
throw new Exception($error . $response, $httpcode);
}
}
thank you
Try this short example:
$secret = 'XXXXXXXXXX';
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/octet-stream', 'Accept: application/octet-stream', 'Content-Disposition: attachment; filename="file.html"'));
curl_setopt($curl, CURLOPT_URL, "https://v2.convertapi.com/html/to/pdf?secret=".$secret);
curl_setopt($curl, CURLOPT_POSTFIELDS, '<!doctype html><html lang=en><head><meta charset=utf-8><title>Conversion test</title></head><body>This is html body</body></html>');
$result = curl_exec($curl);
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == 200) {
file_put_contents("result.pdf", $result);
} else {
print("Server returned error:\n".$result."\n");
}
More examples can be found at: https://repl.it/#ConvertAPI

Instagram Latest post API suddenly stopped working

My instagram feed was working fine but it has stopped working. I am trying to find a solution. First I assume the issue is related to SSL, I even got SSL certificate for my website. But doesn't seem to work. Any help or suggestion is appreciated. Output showed latest image posted on instagram and now shows nothing.1
<div class="col-xs-12 col-sm-6 col-md-5 no-padding">
<div class="insta_img">
<?php
$instagram_id = get_field('instagram_id', 'option') ? get_field('instagram_id', 'option') : '';
$access_token = get_field('instagram_access_token', 'option') ? get_field('instagram_access_token', 'option') : '';
$url = "https://www.instagram.com/{$instagram_id}/?__a=1";
curl_close($ch);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$json = curl_exec($ch);
curl_close($ch);
$data = json_decode($json);
$data = $data->user->media->nodes;
$text = $data[0]->caption;
$created_time = $data[0]->date ;
$images = $data[0]->thumbnail_src;
/
?>
<img src="<?php echo $images; ?>" alt="instagram"/>
<div class="insta_content"><?php echo $text; ?>
<dt><?php echo date('d/m/Y', $created_time); ?></dt>
</div>
</div>
You're not correctly parsing the response. For example, in that response there is no user->media->nodes. You will need to check the response you get from the endpoint, and then parse it correctly to get the images:
$access_token = get_field('instagram_access_token', 'option') ? get_field('instagram_access_token', 'option') : '';
$url = "https://api.instagram.com/v1/users/self/media/recent?access_token={$access_token}";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$json = curl_exec($ch);
curl_close($ch);
$data = json_decode($json);
$data = $data->data;
$text = $data[0]->caption->text;
$created_time = $data[0]->created_time;
$images = $data[0]->images->standard_resolution->url;
Instagram has disabled following capabilities (i.e., deprecated following API's) on 6th April 2018:
Follower List - to read the list of followers and followed-by users
Relationships - to follow and unfollow accounts on a user’s behalf
Commenting on Public Content - to post and delete comments on a user’s behalf on public media
Likes - to like and unlike media on a user’s behalf
Subscriptions - to receive notifications when media is posted
Users Information - to search for and view users' public content
Some information on Public Content returned through hashtag and location
search - Name, Bio, Comments, Commenters, Follower Count, Following Count,
Post Count, and Profile Picture
The access token is working fine. But the post are not visible on the website. The code that I am using is as follow:
$access_token = get_field('instagram_access_token', 'option') ? get_field('instagram_access_token', 'option') : '';
$url = "https://api.instagram.com/v1/users/self/?access_token={$access_token}";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$json = curl_exec($ch);
curl_close($ch);
$data = json_decode($json);
$data = $data->user->media->nodes;
$text = $data[0]->caption;
$created_time = $data[0]->date ;
$images = $data[0]->thumbnail_src;

json api not working in server

i am develop one bitcoin website, i m include the live price tricket, it is working in localhost, when i upload in sever it is not working the code is
<?php
$f="http://api.coindesk.com/v1/bpi/currentprice/INR.json";
$file=file_get_contents($f);
$myjson = json_decode($file);
print "<li>BPI:".$myjson->bpi->INR->rate;
print "<li>disclaimer:".$myjson->disclaimer;
?>
in local i m get result this
after upload i m get this result
Check whether allow_url_fopen is set true in PHP on your live server. If is false ask your server hosting support to set it to true
If server doesn't allow making it true you can try following
$url = "http://api.coindesk.com/v1/bpi/currentprice/INR.json";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($ch);
if (curl_errno($ch)) {
echo curl_error($ch);
echo "\n<br />";
$contents = '';
} else {
curl_close($ch);
}
if (!is_string($contents) || !strlen($contents)) {
echo "Error reading data"l
}
else{
$myjson = json_decode($contents);
echo "<li>BPI:".$myjson->bpi->INR->rate;
echo "<li>disclaimer:".$myjson->disclaimer;
}

import google contact and parse image code

my script for importing gmail contacts work as well , it return name , email , and image but i dont know how to display the image in the html tag . the code json of image returned is like this:
"ôÆÔÔmr½k/8I16¹&ÂÂæöì=†çlk'qËüoÍÒ8äLåLŽáZ³GrF§i)%#’ŽHÔ6ØØ•Âþds˜%h¤‚Ÿ2¨‘Þ—ÖyDÛVºbè¢ýCEuî*RT´l« Ã)GúAËÑï |Î幡XÝÍ-Clr¡Y½’aòg°ùl{Tã‘ÔYúHÁRŠÁ2EKP"7Ô¢R]‹(keëà‡Ž5YÎf­ÈÖ^JÈÈUâ%C´&ü—b¥¹l,Nþã0ÏÂ|CauÒ,°H5#¯CØ‚:†SpÊÖA0ñ€ÂqG|Ûø§.o,ÔW9u×W"²¨‘‘•HÔÄ)TˆÑ?"
Thanks!!! :))
$return = array();
if (!empty($contacts['feed']['entry'])) {
foreach($contacts['feed']['entry'] as $contact) {
//retrieve user photo
if (isset($contact['link'][0]['href'])) {
$url = $contact['link'][0]['href'];
$url = $url . '&access_token=' . urlencode($accesstoken);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_VERBOSE, true);
$image = curl_exec($curl);
curl_close($curl);
}
$return[] = array (
'name'=> $contact['title']['$t'],
'email' => $contact['gd$email'][0]['address'],
'image' =>$image,
);
}
}
foreach ($return as $contact) {
$image=$contact['image'];
echo $image;?>
<br> <br> <br>
<img src="data:image/jpeg;base64,<?php echo $image ?>" /> <br>;
<?php
}

PHP script works fine only on localhost

I'm using this script for parsing an URL that contain a JSON structure. Works fine on localhost (XAMPP) but doesn't work on server side. Anyone have some suggestion about this? I can't find any solutions...
EDIT: The problem is the HOSTING of my website where the script run. It allow communication only on port 80 and 443. There are solutions?
<?php
function session_type($type) {
switch ($type) {
case 0:
return "Booking";
break;
case 1:
return "Pratica";
break;
case 2:
return "Qualifica";
break;
case 3:
return "Gara";
break;
default:
return "Error";
break;
}
}
$server = "http://95.141.37.33:8098/INFO";
$ch = curl_init($server);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$output = curl_exec($ch);
curl_close($ch);
$sjson = json_decode($output, true);
echo 'Current Track: ' . $sjson['track'] . '<br />';
echo 'Name: '. $sjson['name'] . '<br />';
echo 'Utenti Online: '. $sjson['clients'] . '<br />';
foreach ($sjson['cars'] as $key=>$value)
{
echo "Auto:". $value ."<br />";
};
echo 'Port: '. $sjson['port'] . '<br />';
echo 'Sessione: ' . session_type($sjson['session']) . '<br />';
echo 'Timeleft: '. $sjson['timeleft'] . '<br />';
$users = "http://95.141.37.33:8098/JSON|";
$ch = curl_init($users);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$output_user = curl_exec($ch);
curl_close($ch);
$ujson = json_decode($output_user, true);
echo 'Racers: <br />';
foreach ($ujson as $key => $jsons) {
foreach ($jsons as $key => $value) {
echo $value['DriverName'] . '<br />';
}
}
?>
Thanks in advance