Curl, not getting page source code - php

In this script curl is working fine. $output has the page but when I try to echo its source code it displays nothing. I think it has something to do with page headers. Any help!!
header('content-type:text/plain');
$ch = curl_init();
$url = "https://www.amazon.com/gp/css/shiptrack/view.html/ref=TE_SIMP_typ?ie=UTF8&addressID=olqnkqjstjp&latestArrivalDate=1483848000&orderID=103-0753412-5410653&shipmentDate=1483706902&orderingShipmentId=4160817392100&packageId=1";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;

I have modified your code..
Try this.
Remove
header('content-type:text/plain');
and replace your code with mine.
$ch = curl_init();
$url = "https://www.amazon.com/gp/css/shiptrack/view.html/ref=TE_SIMP_typ?ie=UTF8&addressID=olqnkqjstjp&latestArrivalDate=1483848000&orderID=103-0753412-5410653&shipmentDate=1483706902&orderingShipmentId=4160817392100&packageId=1";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
EDIT
Since you want ctrl+u
Here is the PHP code to fetch the html source code of any website specified. fopen function is used to open the website URL. stream_get_contents is used to read the opened URL. The fetched code is displayed inside a textarea.
$domain = 'https://www.amazon.com/gp/css/shiptrack/view.html/ref=TE_SIMP_typ?ie=UTF8&addressID=olqnkqjstjp&latestArrivalDate=1483848000&orderID=103-0753412-5410653&shipmentDate=1483706902&orderingShipmentId=4160817392100&packageId=1';
$handle = fopen($domain, 'r');
$content = stream_get_contents($handle);
/**
// alternative to stream_get_contents
$contents = '';
while (!feof($handle)) {
$content .= fread($handle, 8192);
}
**/
fclose($handle);
/**
* print html content on textarea
*/
echo "<textarea rows='20' cols='80'>$content</textarea>";
output will be like this:
OR
Also if you want to manipulate the retrieved page somehow, you might
want to try some php DOM parser. I find PHP Simple HTML DOM
Parser very easy to use.

Related

I can not make a file get contents link m3u8

I can not do file_get_contents to a m3u8 link, I need it to then make a str_replace in its content, the m3u8 is downloaded without problems from the browser
<?php $url="https://neunlds120dal.akamaized.net/nlds/univisionnow2/univision_mia/as/live/univision_mia_hd_3000_pc.m3u8"; $url2= file_get_contents($url); echo $url2; >
Note that file_get_contents does not work with HTTPS well, try using cURL. For example:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, "https://neunlds120dal.akamaized.net/nlds/univisionnow2/univision_mia/as/live/univision_mia_hd_3000_pc.m3u8");
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
echo $result;

Do not display XML content when read using CURL in PHP

I want to retrieve XML data related to weather.
For that i use this API for it.
https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text=%22Kandy%22)
I tried to di it with CURL in PHP
This is my code
$url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text=%22Kandy%22)";
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url); // get the url contents
$data = curl_exec($ch); // execute curl request
curl_close($ch);
$xml = simplexml_load_string($data);
print_r($xml);
But it retrieve blank output without any of text. Please help me to sortout this problem.
You need to initialize the cURL request like this:
$url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text=%22Kandy%22)";
$ch=curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url); // get the url contents
$data = curl_exec($ch); // execute curl request
curl_close($ch);
$xml = (array)simplexml_load_string($data);
echo "<pre>";
print_r($xml);
echo "<pre>";

cURL data not working properly

I cannot get html output, what could be the problem? the data coming as label, not an html!
function curl_get_contents($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$url = 'https://www.cosmetica.com.tr/tr/arama-sonuclari-8697888010218';
$json = curl_get_contents($url);
preg_match_all('#<a ng-href="(.*?)>#si',$json,$source);
print_r($source[0][0]);
html output: blabla.com.tr/{{slugify(category.name)}}-c-{{category.id}}
links label coming : e.g. {{slugify(subCategory.name)}}
but the links look html e.g. blabla.com//tr/p-art-net-far-karistirma-fircasi-no25-31770

Save browser output to xml file in php

I want to save the below link's output to file.xml but it is not working for me. It just displays the output on another browser.
$url = 'http://www.forexwire.com/feed/full?username=alumfx&password=T7M9Exb4';
$fp = fopen (dirname(__FILE__). '/file.xml', 'w+');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
$ch->save('file.xml');
fclose($fp);
By default CURL's exec function returns the result as standard output. You need to add this to make it return the result as a string:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
then just save it into a variable
$output = curl_exec($ch);
//do whatever with the $output
So that the complete code snippet can look like this:
$ch = curl_init('http://www.forexwire.com/feed/full?username=alumfx&password=T7M9Exb4');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
$output = curl_exec($ch);
curl_close($ch);
file_put_contents('path/to/file', $output);

Not getting HTML using curl, why?

Why am I not getting the HTML code when I use PHP curl? This is my code:
// $content = file_get_contents('http://www.datadiary.com/Company/311734/dimsinstituteofhotelmanagement');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.datadiary.com/Company/311734/dimsinstituteofhotelmanagement');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_COOKIESESSION, false);
$content = curl_exec($ch);
curl_close($ch);
echo $content;
By default curl_exec sends the response to the output (usually the browser). Set the CURLOPT_RETURNTRANSFER option if you want curl_exec to return the result instead:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Relevant manual entries:
http://php.net/curl-exec
http://php.net/curl-setopt
You need to add the option to return the transfer as a string:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

Categories