**my code **
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
$title = $_POST["title"];
$type = $_POST["type"];
$content = $_POST["content"];
$description = $_POST['description'];
$postdata =
array(
'title' => $title,
'type' => $type,
'content' => $content,
'description' => $description,
);
$opts = array('http' =>
array(
'method' => 'POST',
'content' => http_build_query($postdata),
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://localhost:8080/project_war_exploded/admin/blog', false, $context);
echo $result;
}
?>
I am getting error
file_get_contents(http://localhost:8080/project_war_exploded/admin/blog):
Failed to open stream: HTTP request failed! HTTP/1.1 415"**
what is the reason, can anyone help me ?
Like I said - call from the postman and it works fine. Any suggestions?
415 means Unsupported media type. You send www-form-urlencoded what is not expected by the backend.
Add the correct content type to the header, like application/json in this example
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-Type: application/json\r\n".
'content' => http_build_query($postdata),
)
);
Related
Please someone to help me
I'm trying to POST a request with file_get_contents and I receive a 415 code error message. I'm not a pro dev and I want to know what is wrong in my source code. Here is my problem:
I generate a token which is a parameter of the second request
the generated token must be used for the POST request
I receive a 415 Error code message. I don't know how to correct that source
$tok=file_get_contents('https://restapi.bulksmsonline.com/rest/api/v1/sms/gettoken/username/xxxx/password/xxxx');
$toke =json_decode($tok, true);
$token=$toke['token'];
$data = json_encode(
array(
'from' => 'TEST',
'to' => '335546821546',
'type'=> 'Text',
'content'=> 'Test',
'sendDateTime'=> '2020/07/07'
)
);
$options = array('http' =>
array(
'method' => 'POST',
'header' => "Authorization Token " . base64_encode("$token"),
'content' => $data
)
);
$context = stream_context_create($options);
$url = 'https://restapi.bulksmsonline.com/rest/api/v1/sms/send';
$result = file_get_contents($url,false,$context);
If you are getting token and facing issue in post call only then i have noticed that you have missed a few Request Headers:
like Content-type, Content-length, I have check with the different URLs, It is working for me try it
<?php
$tok=file_get_contents('https://restapi.bulksmsonline.com/rest/api/v1/sms/gettoken/username/xxxx/password/xxxx');
$toke =json_decode($tok, true);
$token=$toke['token'];
$data = json_encode(
array(
'from' => 'TEST',
'to' => '335546821546',
'type'=> 'Text',
'content'=> 'Test',
'sendDateTime'=> '2020/07/07'
)
);
$options = array('http' =>
array(
'method' => 'POST',
'header' => "Content-type: application/json\r\n" .
"Content-length: " . strlen($data) . "\r\n" .
"Authorization Token: " . base64_encode("$token") . "\r\n",
'content' => $data
)
);
$context = stream_context_create($options);
$url = 'https://restapi.bulksmsonline.com/rest/api/v1/sms/send';
$result = file_get_contents($url,false,$context);
var_dump($result);
I am getting the error Warning: file_get_contents(https://partner.uat.shopeemobile.com/api/v1/item/categories/get): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in C:\xampp\htdocs\addproduct.php on line 43
<?php
$api_authorization_base_url = "https://partner.shopeemobile.com/api/v1/shop/auth_partner";
$redirect_url = "https://www.google.com";
$partner_id = 840565;
$shopid = 209194;
$key = "aca9b2236136e7aeb8be2b11197a181cd6bc672cbc641f8fe41d0850b7b7f63a";
$token_base_string = $key . $redirect_url;
$token = hash('sha256', $token_base_string);
$data = array(
'id' => $partner_id,
'token' => $token,
'redirect' => $redirect_url
);
$api_authorization_url = $api_authorization_base_url . "?" . json_encode($data);
// echo $api_authorization_url . "\n";
$get_order_by_status_url = "https://partner.uat.shopeemobile.com/api/v1/item/categories/get";
$data = array(
'partner_id' => $partner_id,
'shopid' => $shopid,
'timestamp' => time(),
"language" => "en"
);
$sig_base_string = $get_order_by_status_url . " |" . json_encode($data);
$sig = hash_hmac('sha256', $sig_base_string, $key);
$header = [
'Authorization' => $sig
];
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\n Authorization: " . $sig,
'method' => 'POST /api/v1/item/categories/get HTTP/1.1',
'content' => json_encode($data)
),
);
$context = stream_context_create($options);
// print_r($sig_base_string);
$result = file_get_contents($get_order_by_status_url, false, $context);
print_r($result);
?>
In the $options array you have 'method' and you are passing in invalid stuff, method should be just a valid HTTP request method like POST or GET etc.
I want to use octoparse API.
AND I have error when I want to clear data in octoparse API.
AND this my code
ini_set('max_execution_time', 300);
$url = 'https://dataapi.octoparse.com/api/task/RemoveDataByTaskId?taskId=<mytaskid>';
$options = array(
'http' => array(
'header' => "Authorization: bearer <mykey>",
'method' => 'POST'
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) {
echo 'error';
}
var_dump($result);
My result
Warning: file_get_contents(https://dataapi.octoparse.com/api/task/RemoveDataByTaskId?taskId=<mytaskid>): failed to open stream: HTTP request failed! HTTP/1.1 411 Length Required
And I try to add Content-length But It have an error My new code:
ini_set('max_execution_time', 300);
$url = 'https://dataapi.octoparse.com/api/task/RemoveDataByTaskId?taskId=<mytaskid>';
$a=strlen($url);
$options = array(
'http' => array(
'header' => "Authorization: bearer <my key>",
'method' => 'POST',
'header' => sprintf('Content-Length: %d', $a)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) {
echo 'error';
}
var_dump($result);
My result
Warning: file_get_contents(https://dataapi.octoparse.com/api/task/RemoveDataByTaskId?taskId=<mytaskid>): failed to open stream: HTTP request failed!
Can anyone know how to solve my problem.
Thank.
you are using multiple headers in HTTP. if you are using multiple headers then follow this process
'header' => array(
"Authorization: bearer <my key>",
sprintf('Content-Length: %d', $a)
),
And you can also used this code
$requestHeaders = array(
'Content-type: application/x-www-form-urlencoded',
'Authorization: bearer <my key>',
sprintf('Content-Length: %d', strlen($url));
);
$options = array(
'http' => array(
'method' => 'POST',
'header' => implode("\r\n", $requestHeaders),
)
);
For 2 days I'm having trouble with my PHP script on my server. I've changed nothing and suddenly it didn't work anymore.
Here is the code:
$query = http_build_query($data);
$options = array(
'http' => array(
'header' => "Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: ".strlen($query)."\r\n",
'method' => "POST",
'content' => $query,
),
);
$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n",'method' => 'POST',
'content' => http_build_query($data),));
$contexts = stream_context_create($opts);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $contexts, -1, 40000);
I'm getting these error messages:
Notice: file_get_contents(): Content-type not specified assuming
application/x-www-form-urlencoded in
Warning: file_get_contents(https://mobile.dsbcontrol.de): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server
Error in
But when I try the script locally it works perfectly.
You are passing $contexts to file_get_contents() and that only contains the User-Agent header in the $opts array. All other headers and options are in the $options array which you add in to $context but aren't using. Try:
$query = http_build_query($data);
$options = array(
'http' => array(
'header' => "Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: ".strlen($query)."\r\n".
"User-Agent:MyAgent/1.0\r\n",
'method' => "POST",
'content' => $query,
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context, -1, 40000);
While the existing answers did not work for me, I managed to solve the problem like this:
The PHP Manual says params must be an associative array in the format $arr['parameter'] = $value. Refer to context parameters for a listing of standard stream parameters.
$header = array(
"Content-Type: application/x-www-form-urlencoded",
"Content-Length: ".strlen($postdata)
);
$packet['method'] = "POST";
$packet['header'] = implode("\r\n", $header);
$packet['content'] = $postdata;
$transmit_data = array('http' => $packet);
$context = stream_context_create($transmit_data);
i'm using this
$url = '';
$result = json_decode(file_get_contents($url, false, stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => http_build_query($dataQuery)
)
))), true);
I have web app and api and my login system works with api so it retruns: header status 200 if it success(username and password are correctly) otherwise it returns HTTP/1.0 401 Unauthorized and json message e.x: {success: 0 , "wrong username/password"} (tested with POSTMEN plugin in google chrome)
For e.x if i want to make a request:
$method = "login";
$data = array("user"=>"test", "pass"=>"test");
send_re($method, $data);
this is my function send_re
function send_re($method, $data){
$url = "api.location/".$api_method;
$options = array(
'http' => array(
'method' => 'POST',
'content' => http_build_query($data),
'header'=> "Content-Type: application/x-www-form-urlencoded\r\n"
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
return stripslashes( json_encode( $result) );
}
if my $data are correctly e.x username and password but if they are not I am not getting the error message but this:
A PHP Error was encountered
Severity: Warning
Message: file_get_contents(http://85.10.229.108/home/login): failed to open stream: HTTP request
HTTP/1.0 401 Unauthorized
Filename: libraries/api.php
Is there any way to escape this problem and get the message from api?
function send_re($method, $data){
$url = "api.location/".$api_method;
$options = array(
'http' => array(
'method' => 'POST',
'content' => http_build_query($data),
'header'=> "Content-Type: application/x-www-form-urlencoded\r\n",
'ignore_errors' => true
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
return stripslashes( json_encode( $result) );
}
Just add 'ignore_errors' => true and it's do the trick.
reference: http://php.net/manual/en/context.http.php