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);
Related
$camera_ip = "10.10.10.10";
$image_url = "http://$camera_ip/cgi-bin/image.jpg?camera=right&size=1024x768&quality=60";
$ch = curl_init($image_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$rawdata = curl_exec($ch);
curl_close($ch);
$fp = fopen('./logo.jpg', 'wb');
fwrite($fp, $rawdata);
fclose($fp);
A file is being saved at 7KB but the size is 0x0 ... I can't figure out why the file isn't being saved correctly. My guess is that the file isn't being loaded fully before being saved, but is there a way of ensuring that?
Try using "w" instead of "wb" $fp = fopen('./logo.jpg', 'w');
Or try: file_put_contents('./logo.jpg',$rawdata); (not sure it is binary safe, should be)
And
Check your curl result to see what came back.
$info = var_export(curl_getinfo($ch),true);
file_put_contents('log.txt',$info);
OR
header('content-type: text/plain');
var_export(curl_getinfo($ch));
UPDATE
The curl Request failed (returned false).
Now you need to find out WHY.
For now change your:
curl_setopt($ch, CURLOPT_HEADER, 0);
To:
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
Add these options for trouble shooting:
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$data = curl_exec($ch);
if (curl_errno($ch)){
echo ' Retrieve Base Page Error: ' . curl_error($ch);
}
else {
$info = rawurldecode(var_export(curl_getinfo($ch),true));
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$responseHeader= substr($data,0,$skip);
$data= substr($data,$skip);
echo "HEADER: $responseHeader\n";
echo "\n\nINFO: $info\n\nDATA: $data";
}
You will likely get the "Retrieve Base Page Error:" but it will also tell WHY it failed. If you cannot echo, replace echo with $info .=
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.
I'm trying to copy images from an array of URLs with the following code. The problem is it only saves the last picture, anyone knows why is this?
foreach($photos['data'] as $photo)
{
echo "guardando:";
echo $photo['id'];
$idFoto++;
$fp = fopen('foto'.$idFoto.'.jpg', 'wb');
curl_setopt($ch, CURLOPT_URL, $photo['source']);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
I think your problem is in curl_close($ch), this call should be outside for bucle or maybe you could call curl_init for each photo also could check if curl_setopt is returning true or false:
foreach($photos['data'] as $photo)
{
echo "Guardando $idFoto: $photo['source']\n";
$idFoto++;
$fp = fopen('foto'.$idFoto.'.jpg', 'wb');
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $photo['source']);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
if(curl_exec($ch)===false) echo "Error: ".curl_error()."\n";
fclose($fp);
curl_close($ch);
}
Also note CURLOPT_RETURNTRANSFER is not needed because we are saving to file and not assigning curl_exec return. Could you test please?
Below I have a piece of code to return some information through an API.
$page = 'http://api.duedil.com/sandbox/v2/company/03977902.json? fields=get_all&api_key=***********';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
This prints out the following results http://www.visitrack.co.uk/testdata.php
what i am trying to do is split the results in to individual variables.
Thanks
Something like this should do the trick:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // forces curl_exec() to return page as a string
$json = curl_exec($ch);
curl_close($ch);
$data = json_decode($json, TRUE); // parse json string to array
print_r($data);
I have function to download remote file based on offset and limit.
function get_part_file($url, $offset, $limit){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RANGE, ''.$offset.'-'.$limit.'');
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
I wanna download 1024 bytes from end but this function download whole file. So, how can I do that?
If the remote host does not support Range headers, it doesn't matter what you do: you won't be able to download a specific range.
$ch = curl_init(trim($remoteFile));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RANGE, '0-102399');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false );
$fp = fopen($filepath, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
fclose($fp);
curl_close($ch);