PHP changes the file_get_contents param url - php

I'm using the following line with to get the page content:
$handle = file_get_contents(
"http://www.mywebsite.com/index.php?show=users&action=msg&section=send",
NULL,
NULL,
1000,
19000);
And then, I'm getting the following message:
Warning:
file_get_contents(http://www.mywebsite.com/index.php?show=users&action=msg §ion=send):
failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
(Please take a note at the bolded part).
What happened to it? Why does it changes the url param?

I don't think PHP is changing querystring parameters.
If you are reading that message in the browser, it should be just a matter of how the output is HTML formatted. So, the 403 error you are getting it should not be related to some unwanted url transformation.

Related

file_get_contents Returns NULL

I am trying to fetch some data like first name, last name from LinkedIn. I am using the below code for the same. But I am getting NULL in response. I tried some solutions from SO and other websites but none of them worked.
Below is the code I am using:-
$url = 'http://api.linkedin.com/v1/people/~:(id,first-name,last-name,picture-urls::(original),headline,public-profile-url,location,industry,positions,email-address)?format=json&outh2_access_token='.$accessToken;
$user = file_get_contents($url,false);
var_dump(json_decode($user));
die();
Below is the response that I am getting.
[16-Jul-2018 12:27:20 Europe/London] PHP Warning: file_get_contents(http://api.linkedin.com/v1/people/~:(id,first-name,last-name,picture-urls::(original),headline,public-profile-url,location,industry,positions,email-address)?format=json&outh2_access_token=###########): failed to open stream: HTTP request failed! HTTP/1.0 401 Unauthorized
Basically what I want is to fetch the data from LinkedIn using php, if anybody has done this before please share the solution with me.

Unable to get json from instagram: PHP failed to open stream: HTTP request failed

I'm trying to develop and Instagram application but I'm struggling to get a json inserting the access token via variable.
That's my code:
$personal = json_decode(file_get_contents('https://api.instagram.com/v1/users/self/?access_token={$accesstoken}'));
And the error that I receive is this:
PHP Warning: file_get_contents(https://api.instagram.com/v1/users/self/?access_token={$accesstoken}): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
I already tried to see if I get the variable $accesstoken from the previous form and it's all right, because if I echo-it on the page it pops up.
I tried the function preg_replace to understand if the problem were the white spaces, but nothing.
I don't want to use cURL if it is not mandatory.
What's wrong with the code (and me)?
Thanks in advance!
Edit:
As I answered to #FirstOne: yes, I already tried to put the variable (access token) manually and in this way it works.

How to modify whois contact details?

function modify(.......)
{
$mcontact = file_get_contents( "https://test.httpapi.com/api/contacts/modify.json?auth-userid=$uid&auth-password=$pass&contact-id=$cid&name=$name &company=$company&email=$email&address-line-1=$street&city=$city&country=$country&zipcode=$pincode&phone-cc=$countryCodeList[$phc]&phone=$phone" );
$mdetails = json_decode( $mcontact, true );
return $mdetails;
}
using this modify function, displays warning mesage
Warning: file_get_contents(https://...#hihfg.com&address-line-1=3,dfgdf,fgdf&city=dfgfd&country=India&zipcode=641005&phone-cc=91&phone=756657)
[function.file-get-contents]: failed to open stream: HTTP request failed!
HTTP/1.0 400 Bad request in /home/gfdgfd/public_html/new_one/customer/account/class.whois.php
on line 49
Please help me, modify contact details..
HTTP/1.0 400 Bad request - everything you need to know: You are sending a request the server doesn't like.
Are you sure it accepts a GET request and doesn't require POST? Is the URL correct?
Your URL is wrong. You have an additional space character: &name=$name &company=$company. Remove it.

file_get_contents() and stream_get_contents() fail to open stream

I'm trying to force a file to be downloaded by sending it special headers. In doing so, I have to redirect URL requests for PDF documents through my download script.
I pass a query called $seg3, which is base64_encode()ed before sending, and then base64_decode()ed and urlencoded() when trying to request the file.
I've taken a look at using,
if (ini_get('allow_url_fopen') == '1')
{
$data = file_get_contents(urlencode(base64_decode($seg3)));
}
else
{
$fp = fopen(urlencode(base64_decode($seg3)), 'rb');
$data = stream_get_contents($fp);
fclose($fp);
}
But both file_get_contents() and stream_get_content() fail with:
fopen($URL): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
Yet when I dump the URL that is being sent, I can copy and paste it in my browser and open the file.
It only seems to occur when spaces are in the file, yet the error occurs whether I use urlencode() or not.
It may be that an url returns a 404 error, but still returns regular contents as well. So while the browser may display a regular page, this function will fail because of the result code.
I'm sure this has been fixed already, but if there are spaces urlencode($url) might solve your problem.

Why is this Twitter API script failing?

I'm getting an error from this Twitter script that is causing the rest of the page to not load. Not sure why suddenly this is happening, where it was functioning properly for quite some time.
The script looks like this, and it pulls the users current status:
<?php
$response = new SimpleXMLElement('http://twitter.com/users/show/tuscaroratackle.xml',NULL,TRUE);
echo $response->status->text.'';
?>
Here's another post that I was trying to figure out the answer to another bug which pointed me to this Twitter error.
You can see it here in the footer, or a screengrab of the output: http://cl.ly/33IZ.
The relevant error (which is displayed in the footer of the page you linked to) is:
Warning: SimpleXMLElement::__construct(http://twitter.com/users/show/tuscaroratackle.xml) [simplexmlelement.--construct]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /home5/tuscaror/public_html/footer.php on line 47
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: I/O warning : failed to load external entity "http://twitter.com/users/show/tuscaroratackle.xml" in /home5/tuscaror/public_html/footer.php on line 47
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home5/tuscaror/public_html/footer.php:47 Stack trace: #0 /home5/tuscaror/public_html/footer.php(47): SimpleXMLElement->__construct('http://twitter....', 0, true) #1 /home5/tuscaror/public_html/index.php(119): include('/home5/tuscaror...') #2 {main} thrown in /home5/tuscaror/public_html/footer.php on line 47
The first warning tells you what happened: "HTTP request failed! HTTP/1.1 400 Bad Request".
So, for some reason, your server is failing when making the HTTP request to twitter to retrieve the document "http://twitter.com/users/show/tuscaroratackle.xml". The return code is 400 Bad Request.
I just tried that same request from my web browser, and it worked fine, so either twitter was temporarily "out to lunch" (which does happen from time to time), or there is something unique about your server's network configuration. My first guess would be that somewhere up-stream from your server, someone has installed an HTTP proxy which is (for some unknown reason) blocking your request.
Here's what twitter has to say about it:
400 Bad Request: The request was invalid. An accompanying error message
will explain why. This is the status code will be returned during rate limiting.
Here is twitter's page on Rate Limiting. I suspect that this is your culprit. If you think otherwise, then you might try retrieving the document as a string and examining it before you try to parse it, so you can see what the message is.
This is quick and dirty, but it'll get the message so you can see what's going on:
$str = file_get_contents('http://twitter.com/users/show/tuscaroratackle.xml');
echo $str;
that may fail due to the 400 response code. if so, you'll need to use php curl to get the un-parsed response body.
good luck!

Categories