How to Send Headers Through PHP Proxy? - php

Okay, so I have an issue with an AJAX request. I currently have this URL:
http://www.google.com/images?hl=en&safe=off&gbv=2&tbs=isch%3A1&sa=1&q=cars+imagesize%3A500x500&aq=f&aqi=&aql=&oq=&gs_rfai=&start=0
I then pass it to my proxy script by modifying the string to this:
proxy.php?url=http://www.google.com/images?hl=en&safe=off&gbv=2&tbs=isch%3A1&sa=1&q=cars+imagesize%3A500x500&aq=f&aqi=&aql=&oq=&gs_rfai=&start=0
I need to use the PHP proxy script to grab that page's HTML so that I can then parse through it with javascript. However, the problem is that the headers in that URL are also being sent to the proxy script, and as a result, I get a 'malformed or illegal request' error. I'm pretty sure the two different sets of headers are the problem, because if I just replace the original URL string with 'http://wwww.google.com', the proxy returns the HTML of the page correctly.
So basically, I don't know how to fix this. I'm a complete PHP noob, and I tried escaping the original URL before I appended it to the 'proxy.php?url=', but that doesn't fix anything. Any ideas?
Thanks!

Figured it out- you need to use encodeURIComponent() on the original URL string before you append it to the proxy string.

Related

How to access url after # in php

I am trying to pass some parameters after # in the url like http://developer.rohitkhatri.com/test.php#embed=sdkhfjshdkfhhjk, But I don't know how to access it, I tried many solution from the stackoverflow, here are some examples what I've tried:
$_SERVER['REQUEST_URI'] gives me /test.php
$_SERVER['QUERY_STRING'] gives empty string
$_SERVER['HTTP_REFERER'] gives empty string
also tried printing the whole $_SERVER array but I did not find anything useful.
Any help is appreciated.
Well, there's no way to achieve this, because the part you are trying to access using the php, never goes to the server, what you can do is, just grab the part using the javascript and send it the the server.
Like there can be a middle page, which will redirect to the final url, and while redirecting, It can grab the part after # and send it using ajax.
The browser doesn't send anything that comes after the hash(#) to the server because it is resolved within the browser. You can try by mentioned code.
$hash = '<script>document.write(document.location.hash)</script>';
echo $hash;
output :
//#embed=sdkhfjshdkfhhjk

file_get_contents against symfony2

I have a file_get_contents($myUrl) call from a flat PHP script that isn't working.
If I run $myUrl in a browser it works fine, if I do it over the file_get_contents() it behaves as if the url is incorrect or incomplete.
The myUrl looks like this
https://login.myApp.com/getWifiSettings/ofBoSf593f
Where the last part is a token, and the sym2 webApp behaves as if that token is incorrect, the same way as if I were to paste everything but the last character in the browser (thus producing an incorrect token).
I don't know whether this is an issue caused by the file_get_contents() (do I need any parameters with it to work?) or if it is some security setting in my sym2 installation that denies access for such a call (how does it distinguish between a user's web browser calling the route and a script using file_get_contents to access the route?)
Try to us trim() on the string before you submit it to file_get_contents. It may be that a whitespace character is in the variable and is being submitted and interpreted as part of the URL.

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.

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.

How to get the HTML source code after executing header(location:URL)

My web hosting provider does not permit to use curl FOLLOWLOCATION option so I'm trying to
do it manually by using the header function.
My problem is that I need to keep my PHP script running and to be able to get the redirected URL data for parsing.
How do I do that?
Technically the PHP script continues running after the header () function is called. How you get URL data is another question. Can you not use get_file_contents () or readfile () on the URL?
You read the RAW data the request returns, you check for the redirect header(s), fetch the related URL(s) and do a new get with that URL (dry, rinse, repeat). As simple as that...
Alternatively you could stop being so lazy, check the curl_setopt documentation in the PHP reference manual and find solutions - by reading the comments at the bottom of the page - on how to solve this problem of course.

Categories