Append value to the URL Using curl - php

call multiple url sequentially with php curl
this question is also related to my previous post so ive shared the link above...
Now when i tried append the sessionid in my url execute below code..It didnt run properly..so echoed the url and got following output..
3.0971635097876E+183.0971635097876E+1
So how to append the sid with the url...
Below is how I've tried..
<?php
$response=3097163509787559940;
$url1 = 'http://192.168.1.220/cgi-bin/controller.tcl?sid='+$response+'&type=inverter&inverter=318002N463';
echo "$url1";
$url2 = 'http://192.168.1.220/cgi-bin/overview.tcl?sid='+$response+'&menuParentId=3';
echo "$url2";
$nodes = array('$url1', '$url2');
$node_count = count($nodes);
$curl_arr = array();
$master = curl_multi_init();
for($i = 0; $i < $node_count; $i++){
$url =$nodes[$i];
$curl_arr[$i] = curl_init($url);
curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($master, $curl_arr[$i]);
}
do {
curl_multi_exec($master,$running);
} while($running > 0);
echo "results: ";
for($i = 0; $i < $node_count; $i++){
$results = curl_multi_getcontent ( $curl_arr[$i] );
echo( $i . "\n" . $results . "\n");
}
echo 'done';
?>

Strings are concatenated in PHP with dot (.)
Replace concatenation character + with . in both $url1 and url2
$url1 = 'http://192.168.1.220/cgi-bin/controller.tcl?sid='.$response.'&type=inverter&inverter=318002N463';
Remove quotes while creating array. When enclosed in single quotes, it will render it as string and not as variable.
$nodes = array($url1, $url2);

You need to do 3 changes like below:-
$url1 = "http://192.168.1.220/cgi-bin/controller.tcl?sid=$response&type=inverter&inverter=318002N463";
$url2 = "http://192.168.1.220/cgi-bin/overview.tcl?sid=$response&menuParentId=3";
$nodes = array($url1, $url2);//remove quotes around urls

Related

Extract url meta tags using php

I want to implement facebook like url extract system and i am using php CURL to extract the data
But i am getting url data of only few websites not of all websites
Here is the code:-
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $_POST["url"]);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
// Load HTML to DOM Object
$dom = new DOMDocument();
#$dom->loadHTML($data);
// Parse DOM to get Title
$nodes = $dom->getElementsByTagName('title');
$title = $nodes->item(0)->nodeValue;
// Parse DOM to get Meta Description
$metas = $dom->getElementsByTagName('meta');
$body = "";
for ($i = 0; $i < $metas->length; $i ++) {
$meta = $metas->item($i);
if ($meta->getAttribute('name') == 'description') {
$body = $meta->getAttribute('content');
}
}
// Parse DOM to get Images
$image_urls = array();
$images = $dom->getElementsByTagName('img');
for ($i = 0; $i < $images->length; $i ++) {
$image = $images->item($i);
$src = $image->getAttribute('src');
if(filter_var($src, FILTER_VALIDATE_URL)) {
$image_src[] = $src;
}
}
$output = array(
'title' => $title,
'image_src' => $image_src,
'body' => $body
);
echo json_encode($output);
For few url i got the details like title, description, images but for most of the websites the code do not extract any details do i need to use client side language like jquery

get array out of loop

I need get array out of loop (I need used array, and not, last value)
loop
for ($x = 1; $x < $numero; $x++) {
$frase = $frase_script[$x];
$distrito1 = (explode(',',$frase));
echo $distrito1[0]}
Variable out
$ultimo_nome = $distrito1[0];
I need used array, and not, last value
echo "<br> I need print array, and not, last value".$ultimo_nome;
error: prints the last value and not an array.
Example
all code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.geonames.org/searchJSON?username=country=pt&lang=pt&q=lisbon&fcode=ADM2&adminCode1=14&style=SHORT&maxRows=1000");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/json"
));
$response = curl_exec($ch);
curl_close($ch);
$frase_script = (explode(',',$response));
$frase = $frase_script[0];
$palavra = (explode(':',$frase));
$numero = $palavra[1];
$frase_script = (explode('"name":',$response));
echo '[';
for ($x = 1; $x < $numero; $x++) {
$frase = $frase_script[$x];
$distrito1 = (explode(',',$frase));
echo $distrito1[0]; }
$ultimo_nome = $distrito1[0];
echo $ultimo_nome;
echo ']';
echo "<br> I need print array, and not, last value".$ultimo_nome;
if you need print all the elements but not the lasy you could use
$distrito1 = (explode(',',$frase));
$numElem = count($distrito1);
foreach ($distrito1 as $key => $value){
if ( $key < $numElem-1 ){
echo $value;
} else {
break;
}
}

php how to calculate numbers sum after the loop

I have following loop to calculate comments and likes
function AddCampaignDetails($next=null){
$AccessToken = ACCESS_TOKEN;
$url = "https://api.instagram.com/v1/tags/canonfanatic/media/recent?access_token=".$AccessToken;
if($url !== null) {
$url .= '&max_tag_id=' . $next;
}
/*//Also Perhaps you should cache the results as the instagram API is slow
$cache = './'.sha1($url).'.json';
if(file_exists($cache) && filemtime($cache) > time() - 60*60){
// If a cache file exists, and it is newer than 1 hour, use it
$jsonData = json_decode(file_get_contents($cache));
}else{
$jsonData = json_decode((file_get_contents($url)));
file_put_contents($cache,json_encode($jsonData));
}*/
$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);
$Result = json_decode($Result);
$Data = $Result->data;
//echo "<pre>"; print_r($Data); echo "</pre>";
$CommentsSum = 0;
$LikesSum = 0;
for($i=0; $i<count($Data); $i++){
$CommentsSum += $Data[$i]->comments->count;
$LikesSum += $Data[$i]->likes->count;
}
//echo ' Comments '.$CommentsSum;
//echo ' Likes '.$LikesSum;
echo "<br />";
if(isset($Result->pagination->next_url) && !empty($Result->pagination->next_url)){
$next = $Result->pagination->next_url;
$this->AddCampaignDetails($next);
}else{
$NextUrl = "";
die;
}
return $result;
}
After this loop, I have echo $CommentsSum; variable and get this output
183
306
320
42
Now I want above number sum 851.
Any idea?
Thanks.
Your $Data[$i]->comments->count must be a number, and it's seem that is a string, so it's merging string instead of doing math.
And If you really have a new line between each number (like you said in echo) maybe $Data[$i]->comments->count is equal to "183\n"
use for example :
$CommentsSum += intval($Data[$i]->comments->count)

PHP Foreach loop only displaying last result

I am having a bit of trouble with some code where in an array I have a list of 2 and the function is only displaying the last in the list.
Here is the code:
<?php
function getKeywordPosition($theurl,$thekeywords) {
$theurl = $theurl;
$thekeywords = $thekeywords;
$found = false;
$x = 0;
for($x; $x < 64 && $found == false;)
{
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
. "q=".stripslashes(str_replace(' ', '%20', $thekeywords)).'&start='.$x;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.boo.com');
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body);
$x4 = $x + 4;
$old_x = $x;
for($x; $x < $x4 && $found == false; $x = $x + 1)
{
if (strpos($json->responseData->results[$x-$old_x]->unescapedUrl, strtolower($theurl)) !== false)
{
$found = true;
}
}
// now have some fun with the results...
}
if($found)
{
echo '<strong>'.$theurl.'</strong> is located as the <strong>'.$x.'</strong> result when searching for <strong>'.stripslashes($thekeywords).'</strong>';
echo '<br>';
}
}
$list = array('php.com'=>'php', 'php.com'=>'php');
foreach($list as $key => $value){
getKeywordPosition($key,$value);
}
?>
Why is this not working properly?
Unless this is a badly contrived example, then th issue is you have duplicate keys in your array:
$list = array('php.com'=>'php', 'php.com'=>'php');
This array has a single entry
You coud refactor like so:
$list = array(
array('url'=>'php.net', 'keyword'=>'php'),
array('url'=>'php.net', 'keyword'=>'arrays'),
array('url'=>'php.net', 'keyword'=>'anotherkeyword')
);
foreach($list as $entry){
getKeywordPosition($entry['url'], $entry['keyword']);
}

PHP Call to a member function find()

Code on pastebin
Link on test page
Code:
require_once('simple_html_dom.php');
$file = 'http://testwork.ru/Tempp/domains.php'; // page with table
$SymbolsCountMin = 0;
$SymbolsCountMax = 10;
$SymbolsBackLis = array('-','_','.','0','1','2','3','4','5','6','7','8','9');
$ArrTr = array();
$ArrTd = array();
$ch = curl_init($file);
curl_setopt($ch, CURLOPT_URL, $file );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded'
));
$html = curl_exec($ch);
//$responseInfo = curl_getinfo($ch);
curl_close ($ch);
//var_dump($html);
//$html = file_get_html('http://testwork.ru/Tempp/domains.php');
// Find all tr
$row = 0;
foreach($html->find('tr') as $tr){
if($row!=0){
$row++;
$column = 0;
foreach($tr->find('td') as $td){
$column++;
$text = $td->plaintext;
$ArrTd[$column] = $text;
}
}
if(iconv_strlen($ArrTd[0]) > $SymbolsCountMin && iconv_strlen($ArrTd[0]) < $SymbolsCountMax && !in_array($ArrTd[0], $SymbolsBackList)){
$ArrTr[$row] = $ArrTd;
}
}
$c = '';
foreach($ArrTr as $arr_tr =>$ftr){
$c .='<tr>';
foreach($ftr as $arr_td =>$ftd){
$c .='<td>';
$c .= $ftd;
$c .='<td>';
}
$c .='</tr>';
}
$row_header = '
<table style="text-align:center;">
';
$row_header .= $c;
$row_header .= '
</table>';
echo $row_header;
I get error Fatal error: Call to a member function find() on a non-object in /var/www/seo-main/data/www/testwork.ru/Tempp/parse_domains.php on line 34
Tell me please why i get it error and how make right ?
find() is a member function of
http://simplehtmldom.sourceforge.net/
You have commented out the line
$html = file_get_html('http://testwork.ru/Tempp/domains.php');
With CURL it will return html content not the parsed data.
If you are intend to use CURL then use
$html_data = curl_exec($ch);
curl_close ($ch);
$html = str_get_html($html_data ); // using the returned html from curl to the parser
Then do the rest of the parsing part.
You need to edit the code like this:
// Find all tr
$row = 0;
if(!$html) { // check if $html does exists
foreach($html->find('tr') as $tr){
if($row!=0){
$row++;
$column = 0;
foreach($tr->find('td') as $td){
$column++;
$text = $td->plaintext;
$ArrTd[$column] = $text;
}
}
if(iconv_strlen($ArrTd[0]) > $SymbolsCountMin && iconv_strlen($ArrTd[0]) < $SymbolsCountMax && !in_array($ArrTd[0], $SymbolsBackList)){
$ArrTr[$row] = $ArrTd;
}
}
}
or use a try and catch statement

Categories