This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Drupal login via Rest server
I have been using this code for file get contents with post data but receiving an error
Warning: file_get_contents(http://50.116.19.49/rest/user/login.json): failed to open
stream: HTTP request failed! HTTP/1.0 406 Not Acceptable: Unsupported request content type
application/x-www-form-urlencoded in C:\xampp\htdocs\post.php on line 20
My code is
<?php
$postdata = http_build_query(
array(
'var1' => 'myuser',
'var2' => 'pwd'
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://50.116.19.49/rest/user/login.json', false,
$context);
?>
Can anybody help in this Thanks in advance.
we can use curl instead of function file_get_contents($request);
Here is the code of curl :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$xml_response = curl_exec($ch);
where $request is your url.
This particular server, as it seems, expects to see JSON in the POST data when you call /login.json, so you should rewrite a few things in your code.
Change the $postdata construction:
$postdata = json_encode(array(
'var1' => 'myuser',
'var2' => 'pwd'
));
Change the Content-Type header:
$opts = array('http' => array(
'method' => 'POST',
'header' => 'Content-type: application/json',
'content' => $postdata
));
file_get_contents returns FALSE on error with the HTTP stream wrapper as well. An error means any error condition with the HTTP response, for example a HTTP status code from the 400 range like 406 Not Acceptable: Unsupported request content type in your case.
You can disable the "FALSE on Error" behavior by setting the ignore_errorsHTTP context option to TRUE:
'ignore_errors' = TRUE,
You will then get the response body of the request as the result in error cases, too.
To obtain the status code itself you can make use of the special $http_response_header variable.
For a discussion of these settings and how to parse response headers, please see HEAD first with PHP Streams. However in your case the response body might already contain more information about the problem.
In your specific problem you need to double check that the encoding of the request is supported by the server. As I don't know your server, I can not say much about that. The reference to the error code might shed some light for you. There seems to be a problem with the Content-type: application/x-www-form-urlencoded you are using.
For example as other clever folks on this site have told me, that endpoint is Drupal. If so, the following was suggested in a similar question:
You have to enable application/x-www-form-urlencoded content type of your service endpoint.
Do as follows: Services -> Edit Resources -> select tab "Server" -> enable "application/x-www-form-urlencoded" and that's it.
Hopefully this is of help to you.
Related
I got a new server. I wanted to check if the curl works so I've made 2 files - one that does print_r($_SERVER), and the other that does curl to the first file:
<?php
error_reporting('E_ALL');
$url = $_SERVER['HTTP_HOST'] . '/printServer.php';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_POST => 1 ));
$content = curl_exec($curl);
print $content;
?>
and the outcome is
Bad Request Your browser sent a request that this server could not
understand. Additionally, a 400 Bad Request error was encountered
while trying to use an ErrorDocument to handle the request.
When I change this curl to be get - it does work...
Can anyone help me?
Thanks.
I'm sending the following request to Google Analytics:
http://www.google-analytics.com/collect?v=1&tid=UA-72579327-1&cid=61baecac-8f8c-4dce-bf07-a7efa24a4e47&t=transaction&ti=qcY6pvpWGmP9fHyi&tr=10.00&cd1=Acme Racing&cd2=http://www.domain.co.uk/affiliate/idevaffiliate.php/id=314&tid1=12044762460674420322&url=http://www.nitrotek.co.uk&cd3=A1&cd4=Upgrades&cd5=Acme-Tech &cd6={device}&cd7=&cd8=0&cd9=&cd10=&cd11=&cd12=Nitrotek DSA&cd13=g&cd14=50&cd15=1&cd16=85&cd17=12044762460674420322&cd18=&cd19=http://www.domain.co.uk&cd20=61baecac-8f8c-4dce-bf07-a7efa24a4e47&gclid=
When you go to Google's hit builder and validate the request, it comes out valid.
However, when I send the same request through CURL POST, I just get "400. That’s an error. Your client has issued a malformed or illegal request. That’s all we know.".
POST is the correct method for this request (though I have tried GET just in case) and the Content-Length header is the length of the sent data (string). Here is the code:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $transaction_url, //the same URL string given above
CURLOPT_POST => 1,
CURLOPT_USERAGENT => 'cURL Request'
));
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Length: '.strlen($transaction_url) //length of URL string
));
$trans_resp = curl_exec($curl);
var_dump($trans_resp);
curl_close($curl);
I am using php and I want to create a HTTP request to access some API data. I have a document that says, I need to place the following request
GET /abc/api/Payment HTTP/1.1
Content-Type: application/x-www-form-urlencoded
X-PSK: [App Key]
X-Stamp: [UTC Timestamp]
X-Signature: [HMACSHA256 base 64 string]
Body:
var1, var1
I have app key, I can get UTC Timestamp and I can create signature. I am not sure how to start creating this request? I am using codeingiter. If someone can help with example to set the header and body?
I also tried this url https://www.hurl.it/ to place requests but can't make it work. Any suggestions?
You want to use cURL's CURLOPT_HTTPHEADER option.
http://php.net/manual/en/function.curl-setopt.php
This should get you started
function request($url) {
$ch = curl_init();
$curlOpts = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded",
"X-PSK: [App Key]",
"X-Stamp: [UTC Timestamp]",
"X-Signature: [HMACSHA256 base 64 string]"
),
CURLOPT_FOLLOWLOCATION => true
);
curl_setopt_array($ch, $curlOpts);
$answer = curl_exec($ch);
// If there was an error, show it
if (curl_error($ch)) die(curl_error($ch));
curl_close($ch);
return $answer;
}
I am completely new PHP and want a client program to call an URL web service.I am using file_get_content to get the data.How do add additional headers to the request made using file_get_content.
I also was thinking of using cURL. I wanted to know how cURL can be used to do a GET request.
You can add headers to file_get_contents, it takes a parameter called context that can be used for that:
$context = stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => "Host: www.example.com\r\n" .
"Cookie: foo=bar\r\n"
)
));
$data = file_get_contents("http://www.example.com/", false, $context);
As for cURL, the basic example from the PHP manual shows you how to perform a GET request:
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
My code is using file_get_contents() to make GET requests to an API endpoint. It looks like it is using HTTP/1.0 and my sysadmin says I need to use HTTP/1.1. How can I make an HTTP/1.1 request? Do I need to use curl or is there a better/easier way?
Update
I decided to use cURL since I am using PHP 5.1.6. I ended up forcing HTTP/1.1 by doing this:
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
If I was using 5.3 or later I would have tried doing something like this:
$ctx = stream_context_create(array(
'http' => array('timeout' => 5, 'protocol_version' => 1.1)
));
$res = file_get_contents($url, 0, $ctx);
echo $res;
http://us.php.net/manual/en/context.http.php
Note: PHP prior to 5.3.0 does not
implement chunked transfer decoding.
If this value is set to 1.1 it is your
responsibility to be 1.1 compliant.
Another option I found which might provide HTTP/1.1 is to use the HTTP extension
I'd use cURL in either case, it gives you more control and in particular it gives you the timeout option. That's very important when calling an external API so as not to allow your application to freeze whenever a remote API is down.
Could like this:
# Connect to the Web API using cURL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.url.com/api.php?123=456');
curl_setopt($ch, CURLOPT_TIMEOUT, '3');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xmlstr = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
cURL will use HTTP/1.1 per default, unless you specify something else using curl_setopt($s,CURLOPT_HTTPHEADER,$headers);, where $headers is an array.
Just so others who want to use stream_context_create/file_get_contents know, if your server is configured to use keep-alive connections, the response will not return anything, you need to add 'protocol_version' => 1.1 as well as 'header' => 'Connection: close'. Example below:
$ctx = stream_context_create(array(
'http' => array(
'timeout' => 5,
'protocol_version' => 1.1,
'header' => 'Connection: close'
)
));
$res = file_get_contents($url, 0, $ctx);