writing CVS file using array and dom document in php - php

i am using this code to scrap the amazon.com
$ch = curl_init(); // create a new cURL resource
$url='http://www.amazon.com/s/ref=sr_pg_1?rh=n%3A133140011%2Ck%3Aenglish+literature&sort=paidsalesrank&keywords=english+literature&ie=UTF8&qid=1327432144';
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch); // grab URL and pass it to the browser
//echo $data; ok till now
curl_close($ch);
$dom = new DOMDocument();
#$dom->loadHTML($data); // avoid warnings
$xpath = new DOMXPath($dom);
//getting titles
$book_t = $xpath->query('//div[#class="title"]/a[#class="title"]');
foreach ($book_t as $tag) {
print_r(trim($tag->nodeValue));
echo '<br/>';
}
$author = $xpath->query('//div[#class="title"]/span[#class="ptBrand"]');
foreach ($author as $tag) {
echo '<br/>';
//print_r($tag->nodeValue);
$s=$tag->nodeValue;
print_r(str_replace('by ', '', $s));
echo '<br/>';
}
Up to this step, i am okay, now i want to save this in csv file, but i don't know how to do it can somebody please help me? how should i code it? if you provide me code, my learning will be better.
also, does this code need improvement? if yes, how?

there's a function fputcsv ... you can use it like this:
<?php
$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);
$fp = fopen('file.csv', 'w');//your file csv
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
each array in the array list will be a line in the csv file..

Related

Php web-scraping divs with class do not display images

I'm web-scraping a newspaper online, external url, with this plane php, a class block__item, which is compose of other classes and images
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$url = "https://www.repubblica.it";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);
$doc = new DOMDocument('1.0', 'utf-8');
#$doc->loadHTML($content);
$classname="block__item";
$finder = new DomXPath($doc);
$spaner = $finder->query("//*[contains(#class, '$classname')]");
foreach ($spaner as $value) {
echo $doc->saveXML($value);
}
/?>
Now, images are not displayed; I have the space in the page, the url, but I can't see the image.
I tried with this
$images = $finder->query("(//img/#src)");
foreach ( $images as $image) {
$print = $doc->saveXML($image);
echo $print;
}
and this gave me all the images at the end of the page;
I tried with combine
foreach (array_combine($spaner, $images) as $value => $image){
echo $value, $image;
}
but I receive error.
I tried also with query class and img in the same time, but nothing.
Please, anyone could help me?

How to use json to pass data from a php file to another php file

As mentioned earlier , i am working on the local server as of now in xampp. I have created 2 files index.php and test.php. What i want to achieve is that , index.php will send json data to test.php , with the json data received , the test.php is able to use that json data to turn the statistics into graph.
I am working with the first step , but however , nothing seems to display on my test.php when i tried to do a var_dump($data) , and what i get is NULL. Tried alot of solutions online but none to seems to fix it. I am relatively new to this , so really thanks and appreciate of your help. First php is index.php , second php is test.php.
Do i require a live server as of now in order to see the results in test.php or local server unable to display the result?
<?php
$array = array();
$product = array();
$product[0]['id_product'] = 'A01';
$product[0]['name_product'] = 'Sandal';
$product[0]['price_product'] = '500';
$product[1]['id_product'] = 'A02';
$product[1]['name_product'] = 'Shoes';
$product[1]['price_product'] = '2500';
$array['id'] = '123';
$array['note'] = 'this is my short example';
$array['data'] = $product;
$data = json_encode($array);
$ch = curl_init('http://localhost:8080/practice3/test.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, count($data));
$result = curl_exec($ch);
var_dump($result);
?>
```````````````````````````````````
<?php
$fp = fopen('php://input', 'r');
$raw = stream_get_contents($fp);
$data = json_decode($raw,true);
echo "hello";
echo $data['id'];
echo $data['note'];
foreach ($data['data'] as $key) {
echo 'id_product : '.$key['id_product'].'<br/>';
echo 'name_product : '.$key['name_product'].'<br/>';
echo 'price_product : '.$key['price_product'].'<br/>';
}
?>
````````````````````````````````````

foreach is saving only the first array even print_r printing the result correctly what am missing

I am building a function to download images from an external source, and then write the id of each product has been downloaded along with the path to json file,
The download loop is working correctly but when am trying to save the product id & image path to a text file only the first record is saved.
i tried to print the output before using the file_put_contents function and it turns out am getting the correct result. I am wondering why only the first result is saved not all of them ?
I spent almost all day tryinf to figure it out but no luck. any help will be appreciated.
here is my code:
$url = 'product-test2.json'; // path to JSON file
$data = file_get_contents($url);
$characters = json_decode($data, true);
$i = array_column ($characters , 'Id');
foreach ( $i as $value => $productid){
include 'login.php';
$url = "http://xxxx.com/api/products/image/";
$url .= implode($matches [0]); // get the token form login.php
$url .= '/' . $productid ; //product id
// echo $url . '<br>';
$opts = array ('http' => array (
'methos' => 'GET',
'header' => 'Content-typpe: application/json',
)
);
$context = stream_context_create ($opts);
$urlImage = file_get_contents ($url , false , $context);
$urlImage = substr_replace ($urlImage , "",-1);
$urlImage = substr ($urlImage , 1);
$fopenpath = 'C:\\xampp\htdocs\\test\\api\\images\\' ;
$fopenpath .= $productid . '.jpg';
$fp = fopen ( $fopenpath, 'w');
$c = curl_init ($urlImage);
curl_setopt ($c , CURLOPT_FILE , $fp);
curl_setopt ($c , CURLOPT_HEADER, 0);
curl_setopt ($c , CURLOPT_POST, false);
curl_setopt ($c , CURLOPT_SSL_VERIFYPEER, false);
$rawdata = curl_exec ($c);
fwrite ($fp, $rawdata);
fclose ($fp);
//
$result3= ["id" => $productid ,"image_path" => $fopenpath] ;
$image_file = "images.json";
$output = json_encode ($result3);
print_r ($output);
file_put_contents ($image_file , $output );
};
and here is the result from print_r ($result3);
{"id":2977,"image_path":"C:\\xampp\\htdocs\\test\\api\\images\\2977.jpg"} {"id":2981,"image_path":"C:\\xampp\\htdocs\\test\\api\\images\\2981.jpg"} {"id":3009,"image_path":"C:\\xampp\\htdocs\\test\\api\\images\\3009.jpg"} {"id":3018,"image_path":"C:\\xampp\\htdocs\\test\\api\\images\\3018.jpg"} {"id":11531,"image_path":"C:\\xampp\\htdocs\\test\\api\\images\\11531.jpg"}
as you can see the Print_r is correct all what i want to do is to save the output into file and currently here is the result from file_put_contents only one array :
{"id":11531,"image_path":"C:\\xampp\\htdocs\\test\\api\\images\\11531.jpg"}
can you please help and tell me what am doing wrong i already spend a lot of time on this :)
file_put_contents is the same as opening, writing, and closing the file, which doesn't really make sense to do in a foreach loop. If you really want to use it, I think you could add the FILE_APPEND flag, which will stop it from overwriting itself over and over and over.
An alternative would be to fopen before the foreach loop with the 'a' flag, which will create the file then keep appending lines instead of wiping out the file contents every loop. (And fclose after the foreach.)

Failed read content from json using cUrl in PHP

This is my first time using web service. My PHP server get the json data from .net server where it is encoded to base64.
The problem now is, i failed to read each attribute inside the data:
Array ( [JSONDataResult] => {"file":"iVBORw0KGgoAAAANSUhEUgAAALUAAAEZCAYAAADR+mSIAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAABhuSURBVHhe7ZiBcuM4smz3\/3\/6PkrPjCgfppBMFWB53DgRx2whs4raMWJ7dv938H8\/KVGdjkR1qrNx+13ehftTU9w889V+vVOHqySq05GoTnU2br\/Lu3B\/aoqbZ77ar3fqcJVEdToS1anOxu13eRfuT01x88xX+\/VOHa6SqE5HojrV2bj9Lu\/C\/akpbp75ar\/eeT2cids\/O6cp6Xzad6zeR1PS+bSfovYfz+vhTNz+2TlNSefTvmP1PpqSzqf9FLX\/eF4PZ+L2z85pSjqf9h2r99GUdD7tp6j9x\/N6OBO3f3ZOU9L5tO9YvY+mpPNpP0XtP57XwwpzJ3E5Wd0n6fyn+93c4eZdTlyfuZOo\/HheDyvMncTlZHWfpPOf7ndzh5t3OXF95k6i8uN5PawwdxKXk9V9ks5\/ut\/NHW7e5cT1mTuJyo\/n9bDC3ElcTlb3STr\/6X43d7h5lxPXZ+4kKj+e18MKcydJc0pUZyRRnSr56ZwSl5O07+A+StLcSVR+PK+HFeZOkuaUqM5IojpV8tM5JS4nad\/BfZSkuZOo\/HheDy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/bVBd4Xe\/NrefdSP9gXe\/MrSS\/1KNtsfgXppX7wONsXerPZbDabTYPRv06cmcpH2QOV1RmVP1BZPWP24NX5g3ezzX+U85f56hdeqZ9H2cnj7E6v4vKT2hu9491s8we48wsddV5dkFfnilFWcTvr53ezzR\/gzi9UXYJTcp6NZpKsorJXc6PPrrv5j+N+oUn+6s+KUVfN3jlTn6snqrf5Q4x+oXd+2bws9BU1Y0\/N3TlTnZOaJXOb\/yCvfqF3z9\/t1c9JtzKr92pu8x\/j8YukJ6PswavzispGcy57xau5V+cnLt9sNpvNZrPZbDabzWaz2Ww2m81M\/ve\/\/wclQxXDBQHQjQAAAABJRU5ErkJggg==","fileSize":"8484","returnCode":"0"} )
There are 3 columns:
file
fileSize
returnCode
I have tried to get data using Object, Array or json but always failed.
//$json = json_decode($data, true); //array
//$json = json_decode($data); //object
//echo $result; //JSONDataResult
Below is one of the FAILED method. The full process coding:
<?php
if (isset($_GET['submit'])) {
$project_code = $_GET['project_code'];
$url = "http://1.2.3.4/to/file/process";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl, curlopt_httpheader, array ("Content-type:application/json"));
$result = curl_exec($curl);
curl_close($curl);
$data = file_get_contents($url);
$json = json_decode($data, true); //array
//$json = json_decode($data); //object
//echo $result; //JSONDataResult
//print_r($json);
foreach ($json as $key => $value) {
if (!is_array($value)) {
//always goes here
echo "not array: ".$key . '=>' . $value . '<br/>';
} else {
foreach ($value as $key => $val) {
echo "is array: ".$key . '=>' . $val . '<br/>';
}
}
}
}
?>
Guys help me to read the content. I do not mind using which method as long as i can read each column inside the content. Many-many thank you.
[jangankecamsaya]
Assuming the data you posted in the question is the output of print_r($json);, it is clear that $json is an array that contains only one value (the big JSON) associated to the key JSONDataResult. It basically looks like this:
$json = array('JSONDataResult' => '{"file":"iVBORw0KGgoAAAANSUhEUg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N1e\/Xx352Yz5HFx6Mkoe\/DqvKKy0dwo22w2m82n2f\/tvJlC\/ev+05dqX+rNFHiRPnmx9qXeTMFd6sfn08r5uWavelUyytXZZmNRF+nEZa+6nKuMeu\/s22wuPC5MtTL6nGbVE9U7qX32NpshowvDrH6+m83YsdlEjC5PculeZTN2bDYR7vI88tPK6LPKqpW72Waz2Ww2mz\/F6K\/5O5niztwoU7w7t\/nH4EWon+9k7Dy4M3eiMnYevDu32cjLcqIuzZ2z1TtP1Nlm8+1ivHuRRnMrdp6os80\/jrs4yUV6nFdP2OfnB3fO7s5t\/mFWXqTaeXfnu3Obf5RXl2HGRXI77u58d27zD\/K4CLRy55y5Oqu8yuu5yyv1XOWbzWaz2Wx+Feqv6vOs\/nV+evLq\/ORTmWKUbf4gry5IJe18KmPnwSjb\/FFGF+Ek7fx0dqLOTkbZ5o\/x+GUrK\/z8YNT56exEnZ2Mss0f487leNWpklH+KlO9k1F2os5ORtnmj3HncqQXiIy6NRu9d5SdqLOTUbb5Y9y5HOkFqox67j31s+s+UGcno2zzx7hzOd65QKdklD0Y5a+yes58lG02m81ms\/kI6q\/jevYq33+Nb34t6aUeZZvNr6BzqTebX8njkipP6p9P1Nlm82twl\/bVBd4Xe\/NrefdSP9gXe\/MrSS\/1KNtsfgXppX7wONsXerPZbDabTYPRv06cmcpH2QOV1RmVP1BZPWP24NX5g3ezzX+U85f56hdeqZ9H2cnj7E6v4vKT2hu9491s8we48wsddV5dkFfnilFWcTvr53ezzR\/gzi9UXYJTcp6NZpKsorJXc6PPrrv5j+N+oUn+6s+KUVfN3jlTn6snqrf5Q4x+oXd+2bws9BU1Y0\/N3TlTnZOaJXOb\/yCvfqF3z9\/t1c9JtzKr92pu8x\/j8YukJ6PswavzispGcy57xau5V+cnLt9sNpvNZrPZbDabzWaz2Ww2m81M\/ve\/\/wclQxXDBQHQjQAAAABJRU5ErkJggg==","fileSize":"8484","returnCode":"0"}');
A call to json_decode($json['JSONDataResult'], TRUE) decodes the big string into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RF+nEZa+6nKuMeu/s22wuPC5MtTL6nGbVE9U7qX32NpshowvDrH6+m83YsdlEjC5PculeZTN2bDYR7vI88tPK6LPKqpW72Waz2Ww2mz/F6K/5O5niztwoU7w7t/nH4EWon+9k7Dy4M3eiMnYevDu32cjLcqIuzZ2z1TtP1Nlm8+1ivHuRRnMrdp6os80/jrs4yUV6nFdP2OfnB3fO7s5t/mFWXqTaeXfnu3Obf5RXl2HGRXI77u58d27zD/K4CLRy55y5Oqu8yuu5yyv1XOWbzWaz2Wx+Feqv6vOs/nV+evLq/ORTmWKUbf4gry5IJe18KmPnwSjb/FFGF+Ek7fx0dqLOTkbZ5o/x+GUrK/z8YNT56exEnZ2Mss0f487leNWpklH+KlO9k1F2os5ORtnmj3HncqQXiIy6NRu9d5SdqLOTUbb5Y9y5HOkFqox67j31s+s+UGcno2zzx7hzOd65QKdklD0Y5a+yes58lG02m81ms/kI6q/jevYq33+Nb34t6aUeZZvNr6BzqTebX8njkipP6p9P1Nlm82twl/bVBd4Xe/NrefdSP9gXe/MrSS/1KNtsfgXppX7wONsXerPZbDabTYPRv06cmcpH2QOV1RmVP1BZPWP24NX5g3ezzX+U85f56hdeqZ9H2cnj7E6v4vKT2hu9491s8we48wsddV5dkFfnilFWcTvr53ezzR/gzi9UXYJTcp6NZpKsorJXc6PPrrv5j+N+oUn+6s+KUVfN3jlTn6snqrf5Q4x+oXd+2bws9BU1Y0/N3TlTnZOaJXOb/yCvfqF3z9/t1c9JtzKr92pu8x/j8YukJ6PswavzispGcy57xau5V+cnLt9sNpvNZrPZbDabzWaz2Ww2m81M/ve//wclQxXDBQHQjQAAAABJRU5ErkJggg==',
'fileSize' => 8484,
'returnCode' => 0,
);
The value of file is encoded using base64. The complete code should look like this:
// Decode the JSON
$fileInfo = json_decode($json, TRUE);
// Decode the file content
$content = base64_decode($fileInfo['file']);
// Visually inspect its the decoded file content
print_r($content); // Congratulations, it's a PNG!
// Put the content in a file
file_put_contents('...the file path here...', $content);
// Use getimagesize() to learn about the image type
// an rename the file using the correct termination.
Btw, the value of fileSize is the length of the file field, i.e. the content encoded using base64, not the length of the actual PNG file on disk (as I would expect).
You tried to
$json = json_decode($data[0], true); //array
???
(mind the access on the first field on $data)

How to save the downloaded file to a folder

I write a parser receiving video.No can`t save the resulting video itself to the desired folder to me. How to fix, here's the code.
foreach ($href as $key => $link) {
$doc = file_get_html('https://example.com/'.$link);
foreach($doc->find("#video source") as $el) {
$video[]="https:".$el->src;
}
}
//finally I get
/* array(
"http:example/video1.mp4",
"http:example/video2.mp4",
"http:example/video3.mp4")
*/
$dirSubtitles=$_SERVER['DOCUMENT_ROOT'].'/video/';
foreach ($video as $key=>$address) {
$url = $address;
$path = $dirSubtitles;
$fp = fopen($path."video".$key, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);
curl_close($ch);
fclose($fp);
}
You might want to read about file_put_contents()
I hope this will help you in the right direction.

Categories