i want to scrape data from website using php - php

scraping perticular data from website using php without using any tools,i have tried this code but it is not sufficient-
<?php
$url = 'http://www.google.com';
$output = file_get_contents($url);
echo $output;
?>

you can used curl in php
<?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec();
curl_close($ch);
?>
where $data contain html of given url

Related

file_get_contents phase XML

Trying to run file_get_contents from http://66.85.14.212:7254
Anyone have any suggestions how I could do this to query specific values?
I am getting the value by given code(it's working properly.):-
<?php
$url="http://66.85.14.212:7254/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($data);
echo "<pre>";
print_r($xml);
//for single variable you can write
echo $xml->Name;
?>

File get content not working in php

I am using the following code to retrieve my gmail inbox with PHP, but the code is not working. What am I doing wrong ?
$urls = "https://mygmail#gmail.com:mypassword#gmail.google.com/mail/feed/atom/";
$fileContents = file_get_contents($urls);
print_R($fileContents);
Depending on your PHP configuration, this may be a easy as using:
$urls="https://mygmail#gmail.com:mypassword#gmail.google.com/mail/feed/atom/";
$fileContents = file_get_contents($urls);
However, if allow_url_fopen isn't enabled on your system, you could
read the data via CURL as follows:
<?php
$ch= curl_init();
curl_setopt($ch, CURLOPT_URL, $urls);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($curlSession);
echo $output;
?>

How to scrape data of post genereted url in php using curl

I want a data from other website which url looks like "https://www.example.com/quote/results" after fill all data. I not understand how to pass data to get correct data.I try this code but it show only blank page.
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include('simple_html_dom.php');
$czip=$_POST['czip'];
$dest=$_POST['dest'];
$weight=$_POST['weight'];
$url = 'https://www.example.com/quote/results';
$data = array('collection-zip' =>$czip,
'destination'=>$dest,
'weight' =>$weight);
$ch = curl_init();
$timeout=5;
$ch = curl_init($url);
$data=http_build_query($data);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
echo "<pre>";
print_r($data);
exit;
if($headers['http_code'] == 200){
echo $content;
exit;
}
?>
I am not familiar with curl and web scraping.
if any soultion please share.

PHP - get json data from url

I'm trying to load json data from this url =
http://api.opencagedata.com/geocode/v1/json?query=48.84737%2C2.28605&pretty=1&no_annotations=1&no_dedupe=1&key=b61388b5a248b7cfcaa9579ed290485b
Using file_get_contents works with other json urls but this one is strange. It returns only "{" the first line. Strlen gives 1480 which is right.Substr(2,18) gives "documentation" which is right too. But still i can't echo the entire text. Maybe there's some way to read the text line by line and save it in another string ? The entire text is still fully loaded in the textfile
Here's the php code i tried
<?php
$url = file_get_contents("http://api.opencagedata.com/geocode/v1/json?query=48.84737%2C2.28605&pretty=1&no_annotations=1&no_dedupe=1&key=b61388b5a248b7cfcaa9579ed290485b");
$save = file_put_contents("filename.txt", $url);
echo $url;
?>
Also tried this function but still same.
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;
}
You can get return value with json_decode.
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 json_decode($data,true);
}

How to fetch a webpage with php curl and display that webpage html?

i am new to php and i want to get a webpage html through the use of php curl...i am not getting the output,am just getting a white page.
Can anyone please help?
<?php
$url=$_POST['txturl'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$output = curl_exec($ch);
echo $output;
curl_close($ch);
?>
You don't need to send URL twice
<?php
$url=$_POST['txturl'];
$ch = curl_init();//I have removed it from here
curl_setopt($ch, CURLOPT_URL,$url);// This will do
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$output = curl_exec($ch);
echo $output;
curl_close($ch);
?>
Check if you really have cURL Installed
<?php
echo function_exists('curl_version')?'Yes':'No';
?>

Categories