How to add Content-length To http request in php - php

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),
)
);

Related

Unauthorized Mirosoft Graph request? Calendar.Read

I am trying to execute the following Microsoft Graph request:
$url = 'https://graph.microsoft.com/v1.0/me/calendarview?startdatetime=2020-04-17T12:13:36.933Z&enddatetime=2020-04-24T12:13:36.933Z';
$data = array('grant_type' => 'authorization_code', 'client_id' => '<myclientid>', 'client_secret' => '<myclientsecret>', 'redirect_uri' => 'http://localhost/myapp/request.php', 'code' => '<myauthorisationcode>');
$options = array(
'http' => array(
'header' => "Authorization: Bearer <myaccesstoken>",
'header' => "Host: login.microsoftonline.com",
'header' => "Content-type: application/json",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }
var_dump($result);
Actually I should get information about my calendar now. But I receive the following warning:
Warning: file_get_contents(https://graph.microsoft.com/v1.0/me/calendarview?startdatetime=2020-04-17T12:13:36.933Z&enddatetime=2020-04-24T12:13:36.933Z): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized in C:\xampp\htdocs\myapp\request.php on line 16
bool(false)
In Azure I have added the following 3 API permissions: Calendars.Read, User.Read, User.Read.All.
my API Authorizations
Does anyone know why my request has not been accepted?
Thanks for your help!

PHP sends GET instead of POST

I have to post some data, but the same adress have some GET and POST functions. My PHP is sending a GET instead of a POST.
$apiURL = 'https://myAPI.com.br/api';
$data = http_build_query(array('postdata' => 10));
$uriRequest = $apiURL.'/main';
$options = array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
'https' => array(
'header' => 'Content-type: application/x-www-form-urlencoded',
'method' => 'POST',
'content' => $data
),
);
$context = stream_context_create($options);
$result = file_get_contents($uriRequest, false, $context);
if ($result === FALSE) {
return var_dump($result);
}
return var_dump($result);
I know the ssl part it isnt safe, but it is just for prototyping purpose.
I cant get PHP to POST intestead of GET on the adress 'https://myAPI.com.br/api/main'.
Judging from http://php.net/manual/de/function.stream-context-create.php#74795 the correct way to create a stream context for a https secured page is:
<?php
$context_options = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
. "Content-Length: " . strlen($data) . "\r\n",
'content' => $data
)
);
As you can see we are using 'http' => array... instead of https.

Twitter API works only with one method

$key = 'fd2fdfsdfsdfdsf';
$secret = 'fdsfdsfdsfdsfdsfsdf';
$basic_credentials = base64_encode($key.':'.$secret);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Authorization: Basic '.$basic_credentials."\r\n".
"Content-type: application/x-www-form-urlencoded;charset=UTF-8\r\n",
'content' => 'grant_type=client_credentials'
)
);
$context = stream_context_create($opts);
$pre_token = file_get_contents('https://api.twitter.com/oauth2/token', false, $context);
$token = json_decode($pre_token, true);
if (isset($token["token_type"]) && $token["token_type"] == "bearer")
{
$opts = array('http' =>
array(
'method' => 'GET',
'header' => 'Authorization: Bearer '.$token["access_token"]
)
);
$context = stream_context_create($opts);
$data = file_get_contents('https://api.twitter.com/1.1/search/users.json?q=twitter', false, $context);
var_dump($data); die();
The code above returns false. I have no idea why. When I change the url into:
https://api.twitter.com/1.1/statuses/user_timeline.json?count=200&user_id=43243243
It works perfectly and returns the whole JSON.
Why is that so? I can't get it to work with any other API methods other than this one. The key and secret are the same in each situation. Nothing should be changed.

PHP - Content-type not specified assuming application/x-www-form-urlencoded

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);

php file_get_content returing 401 on failed

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

Categories