Parser problem PHP - php

I am going to write a parser that is going to get information from a website.
However something is wrong with this website and soon as i fetch the information and put it in a file, it's all gibberish.
The website is anidb.net
Could anyone tell me why i get gibberish instead of the HTML?
My code
<?php
$url = 'http://anidb.net/perl-bin/animedb.pl?show=anime&aid=854';
file_put_contents("file.txt", file_get_contents($url));
?>
May i also add that using the browser's View Source function i see the HTML.

I checked the headers on the page that you specified and it's returning:
Content-Encoding: gzip
That means the 'gibberish' you're seeing is indeed gzip encoded. Here's another thread that should help you out:
Decode gzipped web page retrieved via cURL in PHP

Related

PHP cURL output is truncated

I am trying to load a JS file with cURL, but the result is getting truncated. I also tried file_get_contents and it still truncates.
But I can access .js file directly from browser. There isn't anything in the request headers except user agent and referer, which I included in curl request.
What is going on? Is the server messing with me?
The issue was that the browser wasn't showing full code. I echoed the output inside <script> tag, and it was appending DOM elements. Then I split the output into parts and was sure it wasn't being truncated.

PHP header location not working with clean urls

<?php
$result = mysqli_query($link, $query);
$url=$_SERVER['REQUEST_URI'];
if (!$result) {
$url.="/signup";
header("Location:".$url);
} else {
$url.="/home";
header("Location:".$url);
}
?>
Hello friends I am in a bit problem I am doing a project and using clean urls my htaccess is good everything is going good but after a signup form submission when I want to change header location
php gives an error can't change header location
kindly help
I would post a comment but I dont have enought rep.
Anyway, probably you are seeing error about headers already sent. It is common problem and it have nothing to do with clean urls. what you can do?
1. Make sure you are not outputting anything before header() call
Problem is based on fact how web server and HTTP works. When you are outputting server will send headers and content as soon as it is ready. And you may be outputting even whitespaces, so double check this first.
2. Turn on output buffering
If for any reason you need to output something before headers manipulation, you can turn on output buffering. This way server first "buffers" your data and then sents everything out. Hovewer you are paying in load time for this. Use only rarely, when it is really needed!
You can use a ob_start()for this
If that is your PHP script, I can see whitespaces before <?php tag.
<?php
^^^^--- WHITESPACES
These whitespaces will be written to output buffer even before PHP interpreter kicks in, and when you call header() it will give an error. This is because no output should be sent to client before calling header().

How to send out a http reponse

I am a php newbie and I came across some problems when use php with apache.
I don't want to use browser to send or receive http request so that I have to manually deal with this problem. On server side, I can use file_get_content("php://input") to extract body from the http request, but how can I build the http response? The method is "post" and I want to insert a xml string to the response body. Thank you for help!
Example:
See: http://php.net/manual/en/function.header.php
For HTTP codes, Google them up.
Simply echo "whatever" will return that back to the users browser. Remember that PHP is an interpreted language originally for inline scripting (i.e. mixing with HTML) prior to sending to the browser. So a file:
<?php echo "here"; ?>
will just return that reply to the users browser when they go to the appropriate URL. Adding any parsing etc. can be done in addition to this basic application logic.

Using PHP inside Javascript? To shrink URL

I made a bitly url shrinker, and I currently have a Soundcloud Javascript API that outputs a url link of a song. Im trying to shrink it using my shrinker. The shrinker works using this:
<?php echo $bitly->shorten('http://google.com'); ?> //Equals google.com in short url format
The javascript code I'm trying to implement it in is this: Ill go ahead and give you what I tried to do already, that didn't work.
Before I edited:
container.find('span.player-actions').html(
'Soundcloud | Download'
);
After I tried:
container.find('span.player-actions').html(
'Soundcloud | Download'
);
Any suggestions, I'm open to anything. And would love to make this work!
That has been already explained but in case you're new to this concept, there is a simplified explanation.
<?php tags in your code are processed on server before your page is sent to user's browser. Actually browser never receives those tags - they're replaced with PHP output on server and then the resulting page is sent to user.
As a result of some mistake sometimes PHP code makes into user's browser but it behaves as any other non-standard tag - content between <?php and ?> would be invisible to visitor.
JavaScript, on the other hand, operates in user browser with (in our case) what PHP has already output. When you change the page with JavaScript, it's not sent back to server - actually, server is totally unaware of that, so it can't execute the PHP code you're outputting by your JavaScript.
In order to achieve a similar result you need to send an AJAX request from your JavaScript code. It'll basically be another "page request" initiated by your JavaScript, but happening at the background with PHP output not replacing your current page, but arriving into your JavaScript code. This way your JavaScript is outputting PHP output and not PHP code, that's why it is possible.
You cannot call PHP on a string that is generated via javascript since PHP is server side and executed before JavaScript which is client side.
If you want to shorten this string, you'll have to make an ajax call to a php page that will return the shrunk url.

Can not output image into src tags

I have the following:
$imageurl = "<img class='item_thumb'
src='getimagethumbnail.php?imagename=".urlencode($product_image)."&from=".$prodimagedir."'
min-width='150' min-height='150' border='0' class='item_thumb'>";
Which creates the following html:
<img class="item_thumb" border="0" min-height="150" min-width="150"
src="getimagethumbnail.php?imagename=productsmall_1248886833bloggingbok.jpg&
from=products/"/>
However, the image does not show up. I point my browser to that src link and it gives me a bunch of unreadable text which I am assuming is the image meaning that the script getimagethumbnail is working fine. (I am guessing).
But as I said, the image is not appearing at all. What is wrong and what steps can I take to determine the problem?
Just to add, when I point my browser to that src link: It also gives me:
Warning: Cannot modify header information - headers already sent by
(output started at /home/dji/public_html/getimagethumbnail.php:35) in
/home/dji/public_html/includes/functions.php on line 4953
I am assume this is because of the output?? This script was working fine and I have made no changes to it as far as I am aware!
Thanks
You are trying to send the header('Content-Type') command, after outputting whitespace/characters.
You need to make sure that the header command is before anything is printed on the page.
This will work:
header('Content-Type: ....');
readfile('file.png');
This won't
readfile('file.png');
header('Content-Type: ....');
This is because the header command tells the browser what to look for in the content. All of the headers must be sent before any content because that is how the connections works. The browser can't be told what to expect after the content has already been sent.
Open Connection With Server -> Get Headers -> Get Content -> Close Connection
One of the big reasons behind this is encoding. As the content comes through, the browser has to decode it properly. If you send a header in the middle of the page telling the browser that the encoding type is a, when it was processing it like b, things can get really confusing.
So, in order to send the headers properly, you must put the header command before any output.
That error is caused when you print stuff to the output and then attempt to use the header() method. You shouldn't be outputting anything until after you do what you need with header(). Nothing should precede this, not even white-space.
You already have produced some output (on line 35) before setting the header for the image type. This might simply be a whitespace between php tags, or something you forgot to remove.
Your getimagethumbnail.php script is not generating a valid image; it's including text in it (the warning message you quote), which prevents browsers from rendering it. Judging by the error text, I'd guess this is due to changes made either to getimagethumbnail.php or functions.php.
Fundamentally, the problem is that functions.php is attempting to call header() after output has already been sent to the browser, which just plain won't work. You need to check both files and make sure that any calls to header() come before anything else that sends data to the browser.
You may want to turn off the display_errors setting, as any code which generates any warning or error for any reason will cause the problem you're seeing if the warning/error occurs before your header() calls. (Just make sure you have error logging on, so you can still see what's going wrong!)

Categories