file_get_contents not working sometimes? - php

I have a simple file_get_contents using basic http auth:
$opts = array(
'http' => array(
'timeout' => 15,
'header' => array("Authorization: Basic "
. base64_encode("$user:$pwd"))
)
);
$context = stream_context_create($opts);
$data = file_get_contents($url, false, $context);
When I run this inside one PHP box, it arrives without the Authorization header. When I run it in another box, the header arrives.
What kind of info do I need to look for in order to find the cause?

Related

Cookies are not being sent with file_get_contents or CURL requests

I am trying to send a file_get_contents request to a URL with POST data and a cookie set.
My code is like that:
$postdata = http_build_query(
array(
'search' => 'test',
'token' => '0'
)
);
// Create a stream
$opts = array(
'http'=> array(
'method'=>"POST",
'content' => $postdata,
'header' => "Content-Type: application/x-www-form-urlencoded\n"."Cookie: session_hash=123456789"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents('https://example.com', false, $context);
echo $file;
This is posting the data as I can see, but the Cookie is not being sent during the request...
I also tried to use the same request but with CURL and I have the same problem with that.
Any help would be appreciated.
Thanks!
The headers may need to be separated by \r\n rather than just \n. You can also use an array, and they'll be sent properly.
'header' => array("Content-Type: application/x-www-form-urlencoded",
"Cookie: session_hash=123456789")

PHP web push with no library

Is there an easy way to send out push notifications in PHP without using external libraries? Here is my current code. when executed the server gives a 400 UnauthorizedRegistration error. I do not care about how fast or efficient the process is, I am fine with sending one notification at a time.
My PHP code is:
<?php
ini_set("display_errors","1");
$endpoint = "https://fcm.googleapis.com/fcm/send/fImmyvrGiSo:APA91bFg8A4N4GLmJMhouxPfw_R3ocv44uBaNkZu_JMVjAvueISJUJTQROpVxlBnX8PIeJGwE5s1pvLAldtcW-z6WzZ0Ixus2fc3jUADGbJ2L8kAdMv56S5cSmKMsFmPwDsdd2MoCrHC";
$postdata = http_build_query(
array(
"p256dh" => "BAHBGTIPl7M1v8Ui7EaNVlauLmi_jEJOreCQ3YTRu_nWgF4k8nwP5rewGWO86-3wP8yZ86GoKjHQpK_0sls05FI=",
"auth" => "V08681gA2Gs_2x_UNFCN0g=="
)
);
$opts = array('http' =>
array(
'ignore_errors' => true,
'method' => 'POST',
'header' => 'Content-type: application/json',
'content' => $postdata
)
);
$context = stream_context_create($opts);
// here file_get_contents is used to send a POST request
$result = file_get_contents($endpoint, false, $context);
echo $result;
?>
My push subscription is:
{"endpoint":"https://fcm.googleapis.com/fcm/send/dcgCC59wHCE:APA91bHEzFoal6MXgXWt16aSdGHFAeS7D4vre99pCvzU_QWK22YFkmYdUQ5OmiLmUUbuhyiFEDHg61TrpllZ-TcaSgqXXOIcyTUf_0I-ngZ--aH2OFnEIrB2eI-ZRiArHD5LVAAz1EiM","expirationTime":null,"keys":{"p256dh":"BAHBGTIPl7M1v8Ui7EaNVlauLmi_jEJOreCQ3YTRu_nWgF4k8nwP5rewGWO86-3wP8yZ86GoKjHQpK_0sls05FI=","auth":"V08681gA2Gs_2x_UNFCN0g=="}}

fsockopen, cURL, and file_get_contents are slow

I have no clue why... But all the methods I listed in the title are slow. They take about 10 seconds, yet when I visit the sites, they load immediately. Here is one of my codes that uses file_get_conents:
<?php
$search = $_GET['search'];
$postdata = http_build_query(
array(
'searchnode' => '' . $search . ''
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Connection: close',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://google.com', false, $context);
echo $result;
?>
This code is just an example of what I use. But even using this code, it loads slow. I am using "Connection: close" also. Is this possibly a problem with the PHP config, or something else? I am using CentOS with cPanel + WHM. I do have shell and root access.
It could be that the DNS on your server is slow, try this:
replace
$result = file_get_contents('http://google.com', false, $context);
with
$ip = gethostbyname('google.com');
$result = file_get_contents("http://$ip", false, $context);

PHP POST API call

Trying to make a POST API call and I get the error file_get_contents(http://api.turfgame.com/v4/users): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request. API info can be found at http://api.turfgame.com
<?php
$data = array("name" => "DIProgan");
$url = 'http://api.turfgame.com/v4/users';
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $data ),
'header'=> "Content-Type: application/json\r\n" .
"Accept: application/json\r\n"
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$response = json_decode( $result );
var_dump($response);
?>
Using XAMPP & PHP 5.4.4. I tried using "requests for PHP" since I liked the Python version of it but it just complains about not finding hook functions. People seem to hate cURL and from the little I need it seemed easier without it. Maybe not. Help?
It seems like you have just created an array where you have put your parameter. It should be stored within an object first.

php file_get_contents authorization header

Can anyone give me any explanation for why this authorization function for a private bitbucket repository is working on my local machine (running PHP Version 5.3.17) but is not authorizing on my remote server (running PHP Version 5.3.20)
I'm not getting an error per se -- i'm just getting a "forbidden" response from bitbucket. But everything works great running from my local server.
function bitBucketConnect($url){
global $bitPassword;
global $bitUsername;
$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode("$bitUsername:$bitPassword")
)
));
// Make the request
return file_get_contents($url, false, $context);
}
Your proxy will respond that authentication is required. You may scratch your head and think "but I'm providing authentication!"
The issue is that the 'header' value is only applicable to http connections. So to authenticate on a proxy, you first have to pull a file from HTTP, before the context is valid for using on FTP.
<?php
$opts = array('ftp' => array(
'proxy' => 'tcp://vbinprst10:8080',
'request_fulluri'=>true,
'header' => array(
"Proxy-Authorization: Basic $auth"
)
),
'http' => array(
'proxy' => 'tcp://vbinprst10:8080',
'request_fulluri'=>true,
'header' => array(
"Proxy-Authorization: Basic $auth"
)
)
);
$context = stream_context_create($opts);
$s = file_get_contents("http://www.example.com",false,$context);
$s = file_get_contents("ftp://anonymous:anonymous#ftp.example.org",false,$context);
?>

Categories