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);
?>
Related
I am new to PHP. I am trying to make a HTTPS POST request from PHP to an external server. Based on the posts on the Internet I have the following code:
// Create POST data.
$post_data = array(
'roomId' => $space_token,
'text' => $message
);
$output .= json_encode($post_data);
// Creating context
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Authorization: Bearer {$api_access_token}\r\n".
"Content-Type: application/json\r\n",
'content' => json_encode($post_data)
)
));
$response = fopen($api_url, 'r', false, $context);
However, the request is made with HTTP/1.0 instead of HTTP/1.1, and the server that i am trying to reach returns 502 Bad Gateway response when it receives HTTP/1.0 requests.
PHP Version: 5.4.16
HTTPD Version: Apache/2.4.6 (CentOS)
Not sure what I am doing wrong.
You need insert to header 'protocol_version' => 1.1
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=="}}
This is driving me nuts!
If the method is POST, why Azure is returning an "AADSTS90056: This endpoint only accepts POST, OPTIONS requests. Received a GET request" error?
Code:
$url='http://login.microsoftonline.com/common/oauth2/v2.0/token';
$data = array('code'=>$code,'resource'=>$resource,'redirect_uri' => $redirect_uri, 'client_id' => $client_ID, 'scope' => $scope, 'grant_type' => $grant_type, 'client_secret' => $client_secret);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
Azure OAuth 2.0 endpoint is only accessible via HTTPS and that HTTPS is enforced via a 302 redirect if we make a request to it with plain HTTP. This will cause the HTTP verb to change to GET. So, you'll need to use HTTPS instead of HTTP to make it work.
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?
I'm developing a web application (using GAE for PHP) that notifies android clients (via GCM - Google Cloud Messaging) when some content are available for download.
The following PHP script should do job:
$json = array(
'data' => array( ... ),
'registration_ids' => array( ... )
);
$data = json_encode( $json );
$context = array(
'http' => array(
'method' => 'post',
'header' => 'Authorization: key=MY_SECRET_KEY' . "\r\n" .
'Content-Type: application/json' . "\r\n",
'content' => $data
)
);
$context = #stream_context_create($context);
$result = #file_get_contents("https://android.googleapis.com/gcm/send", false, $context);
The above code runs correctly when the app is deployed, but do not when running on my local development environment.
On local development environment $result is null and the file_get_contents "echo" the following warning failed to open stream: HTTP request failed! HTTP/1.0 405 Method Not Allowed.
I finally figured out what was happening.
The HTTP/1.0 405 Method Not Allowed was related with the 'method' => 'post'.
Belive me, simply changing it to 'method' => 'POST' (note the uppercase!) did the trick.