I am trying to use file_get_contents to "post" to a url and get auth token. The problem I am having is that it returns a error.
Message: file_get_contents(something): failed to open stream: HTTP request failed! HTTP/1.1 411 Length Required.
I am not sure what is causing this but need help. Here is the function.
public function ForToken(){
$username = 'gettoken';
$password = 'something';
$url = 'https://something.com';
$context = stream_context_create(array (
'http' => array (
'header' => 'Authorization: Basic ' . base64_encode("$username:$password"),
'method' => 'POST'
)
));
$token = file_get_contents($url, false, $context);
if(token){
var_dump($token);
}else{
return $resultArray['result'] = 'getting token failed';
}
}
I tried it with POSTMAN, and it works, so only problem I am having is that why isnt it working with file_get_contents.
Cannot command so i will do it this way. If you use Postman why don't you let Postman generate the code for you. In postman you can click code at a request and you can even select in what coding language you wanne have it. Like PHP with cURL:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://somthing.com/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"password: somthing",
"username: gettoken"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Hope this will help!
I can not comment because of my reputation;
In some servers file_get_content is not available...
You may try with CURL like this
Your request havent any data and content length is missing.
You can try this:
public function ForToken(){
$username = 'gettoken';
$password = 'something';
$url = 'https://something.com';
$data = array('foo' => 'some data');
$data = http_build_query($data);
$context_options = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
. "Authorization: Basic " . base64_encode("$username:$password") . "\r\n"
. "Content-Length: " . strlen($data) . "\r\n",
'content' => $data
)
);
$context = context_create_stream($context_options);
$token = file_get_contents($url, false, $context);
if(token){
var_dump($token);
}else{
return $resultArray['result'] = 'getting token failed';
}
}
Here is similar problem:
How to fix 411 Length Required error with file_get_contents and the expedia XML API?
RFC2616 10.4.12
https://www.rfc-editor.org/rfc/rfc2616#section-10.4.12
Code: (Doesn't work, see below)
public function ForToken(){
$username = 'gettoken';
$password = 'something';
$url = 'https://something.com';
$context = stream_context_create(array (
'http' => array (
'header' => 'Authorization: Basic ' . base64_encode("$username:$password") . 'Content-Length: ' . strlen($url) . '\r\n',
'method' => 'POST'
)
));
$token = file_get_contents($url, false, $context);
if(token){
var_dump($token);
}else{
return $resultArray['result'] = 'getting token failed';
}
}
2nd Try:
public function ForToken(){
$username = 'gettoken';
$password = 'something';
$url = 'https://something.com';
$context_options = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n" . "Authorization: Basic " . base64_encode("$username:$password") . "Content-Length: " . strlen($url) . "\r\n",
'content' => $url
)
);
$context = stream_context_create($context_options);
$token = file_get_contents($url, false, $context);
if(token){
var_dump($token);
}else{
return $resultArray['result'] = 'getting token failed';
}
}
This is from patryk-uszynski's answer formatted with the OP's code.
Related
I have a problem with my code. So I have already read many posts on Stackoverflow and the documentation of eBay APIs. But I still can't get it to work.
I have already tried to change the 'setopt' variables but it doesn't work.
Note: The Code is inside a constructor function.
$headers = [
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Basic ' . base64_encode($user_key . ":" . $user_secret),
];
$body = http_build_query([
'grant_type' => 'authorization_code',
'code' => $oauth,
'redirect_uri' => $runame,
]);
switch($action){
case "import-products":
$cmd = "https://api.ebay.com/oauth/api_scope/sell.inventory";
break;
default:
$cmd = "";
break;
}
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => $headers,
));
$response = curl_exec($curl);
$err = curl_error($curl);
if(curl_errno($curl)){
print curl_error($curl);
}
curl_close($curl);
if($err){
echo "cURL Error: ".$err;
}else{
echo "cURL Successful: ";
var_dump(json_decode($response, true));
echo "\n";
}
The result is always 'cURL Successful: NULL' but I want to get my entire inventory in JSON Format instead of the NULL.
I'm currently utilizing a php app to upload images to the Google cloud storage platform, however, unlike on my local server, I am having tremendous trouble figuring out how to make this work.
Here is exactly what I am trying to do:
Write the Path of the image to my Google cloud SQL
Actually upload the image to the Google cloud storage platform
write a script calling on the image, from the saved SQL path, to then post to my site
Can anyone point in the right direction?
Thanks!
Something like this worked for me with the form on GAE - upload photo from Form via php to google cloud storage given your folder permission are set...
// get image from Form
$gs_name = $_FILES["uploaded_files"]["tmp_name"];
$fileType = $_FILES["uploaded_files"]["type"];
$fileSize = $_FILES["uploaded_files"]["size"];
$fileErrorMsg = $_FILES["uploaded_files"]["error"];
$fileExt = pathinfo($_FILES['uploaded_files']['name'], PATHINFO_EXTENSION);
// change name if you want
$fileName = 'foo.jpg';
// put to cloud storage
$image = file_get_contents($gs_name);
$options = [ "gs" => [ "Content-Type" => "image/jpeg"]];
$ctx = stream_context_create($options);
file_put_contents("gs://<bucketname>/".$fileName, $gs_name, 0, $ctx);
// or move
$moveResult = move_uploaded_file($gs_name, 'gs://<bucketname>/'.$fileName);
The script to call the image to show on your site is typical mysqli or pdo method to get filename, and you can show the image with...
<img src="https://storage.googleapis.com/<bucketname>/<filename>"/>
in case anyone may be interested, I made this, only upload a file, quick & dirty:
(I do not want 500+ files from the php sdk just to upload a file)
<?php
/**
* Simple Google Cloud Storage class
* by SAK
*/
namespace SAK;
use \Firebase\JWT\JWT;
class GCStorage {
const
GCS_OAUTH2_BASE_URL = 'https://oauth2.googleapis.com/token',
GCS_STORAGE_BASE_URL = "https://storage.googleapis.com/storage/v1/b",
GCS_STORAGE_BASE_URL_UPLOAD = "https://storage.googleapis.com/upload/storage/v1/b";
protected $access_token = null;
protected $bucket = null;
protected $scope = 'https://www.googleapis.com/auth/devstorage.read_write';
function __construct($gservice_account, $private_key, $bucket)
{
$this->bucket = $bucket;
// make the JWT
$iat = time();
$payload = array(
"iss" => $gservice_account,
"scope" => $this->scope,
"aud" => self::GCS_OAUTH2_BASE_URL,
"iat" => $iat,
"exp" => $iat + 3600
);
$jwt = JWT::encode($payload, $private_key, 'RS256');
// echo "Encode:\n" . print_r($jwt, true) . "\n"; exit;
$headers = array(
"Content-Type: application/x-www-form-urlencoded"
);
$post_fields = "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=$jwt";
// $post_fields = array(
// 'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
// 'assertion' => $jwt
// );
$curl_opts = array(
CURLOPT_URL => self::GCS_OAUTH2_BASE_URL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $post_fields,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1
);
$curl = curl_init();
curl_setopt_array($curl, $curl_opts);
// var_dump($curl); exit;
$response = curl_exec($curl);
if (curl_errno($curl)) {
die('Error:' . curl_error($curl));
}
curl_close($curl);
$response = json_decode($response, true);
$this->access_token = $response['access_token'];
// echo "Resp:\n" . print_r($response, true) . "\n"; exit;
}
public function uploadObject($file_local_full, $file_remote_full, $content_type = 'application/octet-stream')
{
$url = self::GCS_STORAGE_BASE_URL_UPLOAD."/$this->bucket/o?uploadType=media&name=$file_remote_full";
if(!file_exists($file_local_full)) {
throw new \Exception("$file_local_full not found.");
}
// $filesize = filesize($file_local_full);
$headers = array(
"Authorization: Bearer $this->access_token",
"Content-Type: $content_type",
// "Content-Length: $filesize"
);
// if the file is too big, it should be streamed
$curl_opts = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => file_get_contents($file_local_full),
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1
);
// echo "curl_opts:\n" . print_r($curl_opts, true) . "\n"; exit;
$curl = curl_init();
curl_setopt_array($curl, $curl_opts);
$response = curl_exec($curl);
if (curl_errno($curl)) {
$error_msg = curl_error($curl);
throw new \Exception($error_msg);
}
curl_close($curl);
return $response;
}
public function uploadData(string $data, string $file_remote_full, string $content_type = 'application/octet-stream')
{
$url = self::GCS_STORAGE_BASE_URL_UPLOAD."/$this->bucket/o?uploadType=media&name=$file_remote_full";
// $filesize = strlen($data);
$headers = array(
"Authorization: Bearer $this->access_token",
"Content-Type: $content_type",
// "Content-Length: $filesize"
);
$curl_opts = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1
);
// echo "curl_opts:\n" . print_r($curl_opts, true) . "\n"; exit;
$curl = curl_init();
curl_setopt_array($curl, $curl_opts);
$response = curl_exec($curl);
if (curl_errno($curl)) {
$error_msg = curl_error($curl);
throw new \Exception($error_msg);
}
curl_close($curl);
return $response;
}
public function copyObject($from, $to)
{
// 'https://storage.googleapis.com/storage/v1/b/[SOURCEBUCKET]/o/[SOURCEOBJECT]/copyTo/b/[DESTINATIONBUCKET]/o/[DESTINATIONOBJECT]?key=[YOUR_API_KEY]'
$from = rawurlencode($from);
$to = rawurlencode($to);
$url = self::GCS_STORAGE_BASE_URL."/$this->bucket/o/$from/copyTo/b/$this->bucket/o/$to";
// $url = rawurlencode($url);
$headers = array(
"Authorization: Bearer $this->access_token",
"Accept: application/json",
"Content-Type: application/json"
);
$payload = '{}';
$curl_opts = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1
);
// echo "curl_opts:\n" . print_r($curl_opts, true) . "\n"; exit;
$curl = curl_init();
curl_setopt_array($curl, $curl_opts);
$response = curl_exec($curl);
// echo '<pre>'; var_dump($response); exit;
if (curl_errno($curl)) {
$error_msg = curl_error($curl);
throw new \Exception($error_msg);
}
curl_close($curl);
return $response;
}
public function deleteObject($name)
{
// curl -X DELETE -H "Authorization: Bearer OAUTH2_TOKEN" "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME"
//
$name = rawurlencode($name);
$url = self::GCS_STORAGE_BASE_URL."/$this->bucket/o/$name";
$headers = array(
"Authorization: Bearer $this->access_token",
"Accept: application/json",
"Content-Type: application/json"
);
$curl_opts = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "DELETE",
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1
);
// echo "curl_opts:\n" . print_r($curl_opts, true) . "\n"; exit;
$curl = curl_init();
curl_setopt_array($curl, $curl_opts);
$response = curl_exec($curl);
// echo '<pre>'; var_dump($response); exit;
if (curl_errno($curl)) {
$error_msg = curl_error($curl);
throw new \Exception($error_msg);
}
curl_close($curl);
return $response;
}
public function listObjects($folder)
{
// curl -X GET -H "Authorization: Bearer OAUTH2_TOKEN" "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o"
//
$folder = rawurlencode($folder);
$url = self::GCS_STORAGE_BASE_URL."/$this->bucket/o?prefix=$folder";
$headers = array(
"Authorization: Bearer $this->access_token",
"Accept: application/json",
"Content-Type: application/json"
);
$curl_opts = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1
);
// echo "curl_opts:\n" . print_r($curl_opts, true) . "\n"; exit;
$curl = curl_init();
curl_setopt_array($curl, $curl_opts);
$response = curl_exec($curl);
// echo '<pre>'; var_dump($response); exit;
if (curl_errno($curl)) {
$error_msg = curl_error($curl);
throw new \Exception($error_msg);
}
curl_close($curl);
return $response;
}
}
One site requires login for three variables: username, password and token. This data is sent via POST.
token is also sent through a section.
I have all three variables, and I want to log in via file_get_contents.
How can that data be sent and the authentication is successful?
function file_post_contents()
{
$url = 'http://site.com/auth.php?' . session_name() . '=' . session_id();
$login = 'jhon';
$senha = 'doe';
$token = '123456';
$_SESSION["token"] = $token;
$postdata = http_build_query(
array(
'login' => $login,
'senha' => $senha,
'token' => $token,
$_SESSION["token"] => $token
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
if($login && $senha)
{
$opts['http']['header'] = ("Authorization: Basic " . base64_encode("$login:$senha"));
}
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);
}
Modified code. It does not work.
function file_post_contents()
{
$url = 'http://site.com/auth.php?' . session_name() . '=' . session_id();
$login = 'jhon';
$senha = 'doe';
$token = '123456';
$_SESSION["token"] = $token;
$postdata = http_build_query(
array(
'login' => $login,
'senha' => $senha,
'token' => $token
//, $_SESSION["token"] => $token
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded'
. "\r\n" . "Authorization: Basic " . base64_encode("$login:$senha"). "\r\n",
'content' => $postdata
)
);
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);
}
Your headers might be set wrong, try adding \r\n like so
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => $postdata
)
);
and later down the code
$opts['http']['header'] = $opts['http']['header'] . "Authorization: Basic " . base64_encode("$login:$senha") . "\r\n";
You're overriding $opts['http']['header'] with the Basic auth. You're not adding it to the Content-type, you're replacing Content-type with it.
I am trying to POST JSON content to a remote REST endpoint, however the 'content' value appears to be empty on delivery. All other headers etc are being received correctly, and the web service tests successfully with a browser based test client.
Is there a problem with my syntax below where I specify the 'content' field?
$data = array("username" => "duser", "firstname" => "Demo", "surname" => "User", "email" => "example#example.com");
$data_string = json_encode($data);
$result = file_get_contents('http://test.com/api/user/create', null, stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => array('Content-Type: application/json'."\r\n"
. 'Authorization: username:key'."\r\n"
. 'Content-Length: ' . strlen($data_string) . "\r\n"),
'content' => $data_string)
)
));
echo $result;
This is the code I always use and it looks pretty similar (though this is of course for x-www-form-urlencoded).
Perhaps your username:key needs to be base64_encode'd.
function file_post_contents($url, $data, $username = null, $password = null)
{
$postdata = http_build_query($data);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
if($username && $password)
{
$opts['http']['header'] .= ("Authorization: Basic " . base64_encode("$username:$password"));
}
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);
}
The question was about json, why the accepted answer is about x-www-form?
Json has many cool stuff to struggle about, like utf8_encode
function my_utf8_encode(array $in): array
{
foreach ($in as $key => $record) {
if (is_array($record)) {
$in[$key] = my_utf8_encode($record);
} else {
$in[$key] = utf8_encode($record);
}
}
return $in;
}
function file_post_contents(string $url, array $data, string $username = null, string $password = null)
{
$data = my_utf8_encode($data);
$postdata = json_encode($data);
if (is_null($postdata)) {
throw new \Exception('decoding params');
}
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/json',
'content' => $postdata
)
);
if (!is_null($username) && !is_null($password)) {
$opts['http']['header'] .= "Authorization: Basic " . base64_encode("$username:$password");
}
$context = stream_context_create($opts);
try {
$response = file_get_contents($url, false, $context);
} catch (\ErrorException $ex) {
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
}
if ($response === false) {
throw new \Exception();
}
return $response;
}
The earlier response of
function file_post_contents($url, $data, $username = null, $password = null) {
$postdata = http_build_query($data);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
if($username && $password)
{
$opts['http']['header'] = ("Authorization: Basic " . base64_encode("$username:$password"));
}
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);}
is incorrect. This function works sometimes, but it is inaccurate and will fail if you're not using the Content-type of application/x-www-form-urlencoded and you pass in a username and password.
It's working for the writer because application/x-www-form-urlencoded is the default Content-type, but his handling of the username and password is overwriting the earlier declaration of content type.
Here is the corrected function:
function file_post_contents($url, $data, $username = null, $password = null){
$postdata = http_build_query($data);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => $postdata
)
);
if($username && $password)
{
$opts['http']['header'] .= ("Authorization: Basic " . base64_encode("$username:$password")); // .= to append to the header array element
}
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);}
Note the line:
$opts['http']['header' .= (dot equals to append to the array element.)
If cURL is unavailable I want to send HTTP requests using fopen. I got the code for a class from a PACKT RESTful PHP book but it does nto work. Any ideas why?
if ($this->with_curl) {
//blah
} else {
$opts = array (
'http' => array (
'method' => "GET",
'header' => array($auth,
"User-Agent: " . RESTClient :: USER_AGENT . "\r\n"),
)
);
$context = stream_context_create($opts);
$fp = fopen($url, 'r', false, $context);
$result = fpassthru($fp);
fclose($fp);
}
return $result;
}
The HTTP context options are laid out here: http://www.php.net/manual/en/context.http.php
The header option is a string, so as #Mob says you should be using \r\n and string concatenation rather than an array. However, user_agent is a valid key, so you could just use that instead.
I'm guessing that the contents of the $auth variable is something along the lines of Authorization: blah - i.e. standard header format?
The below code is a working example. Note that I've changed your fpassthru() (which outputs the content to the browser, and does not store it to $result) to a fread() loop. Alternatively you could have wrapped the fpassthru() call with ob_start(); and $result = ob_get_clean();
<?php
class RESTClient {
const USER_AGENT = 'bob';
}
$url = 'http://www.example.com/';
$username = "fish";
$password = "paste";
$b64 = base64_encode("$username:$password");
$auth = "Authorization: Basic $b64";
$opts = array (
'http' => array (
'method' => "GET",
'header' => $auth,
'user_agent' => RESTClient :: USER_AGENT,
)
);
$context = stream_context_create($opts);
$fp = fopen($url, 'r', false, $context);
$result = "";
while ($str = fread($fp,1024)) {
$result .= $str;
}
fclose($fp);
echo $result;
You're mixing this. Shouldn't it be ::
$opts = array (
'http' => array (
'method' => "GET",
'header' => $auth . "\r\n" . //removed array()
"User-Agent: " . RESTClient :: USER_AGENT . "\r\n" )
)
Here's an example of setting headers from the PHP manual
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);