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.
Related
I'm trying to test retrieving an USPTO dataset for a number of records for oa_rejections using their open API:
https://developer.uspto.gov/ds-api-docs/index.html?url=https%3A//developer.uspto.gov/ds-api/swagger/docs/oa_rejections.json/v2#!/oa_rejections/perform_search
I have made the following php script. It ends up displaying just "Internal Server Error1":
<?
$params=['criteria'=>'*%3A*', 'start'=>'100', 'rows'=>'3'];
$defaults = array(
CURLOPT_URL => 'https://developer.uspto.gov/ds-api/oa_rejections/v2/records',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $params,
);
$ch = curl_init();
curl_setopt_array($ch, $defaults);
$contents = curl_exec($ch);
echo $contents;
?>
Is there anything vital or obvious that I'm missing, or perhaps I cannot use this syntax at all to retrieve this data?
I'm trying to upload a file using the openload API that can be found here.
When I upload some file I always get a JSON response of success (code: 200):
Unfortunately when I check the file status (using both API and website) it tells me that there was an error in upload. Here the JSON response (code 200):
Here is how I send the request. Where's the error? I tried using both the urlencode function and not.
$user = "...";
$psw = "...";
$link = urlencode("https://google.com/favicon.ico");
$url = "https://api.openload.co/1/remotedl/add?login=$user&key=$psw&url=$link";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
$resp = curl_exec($curl);
curl_close($curl);
echo $resp;
EDIT: Switched to https and added CURLOPT_FOLLOWLOCATION => true, still no success.
I think you are hitting a http 301 redirect while using curl
http://google.com/favicon.ico
to
https://www.google.com/favicon.ico
If that case curl option CURLOPT_FOLLOWLOCATION might help.
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'm trying to get the response back from an API using curl in php but for some reason it fails. However, when I open the API call link in a browser it works fine. Furthermore, if I try to get the response with curl after I've opened the link in the browser curl works but if I change the $url variable to a new link it again stops working until I open it in the browser again.
Here is my code, don't worry about the api key it's just a test:
<?php
set_time_limit(30);
$API_KEY = "ak_wujpBbfefrmxDleyAmnqtFpqAcmey";
$url = 'https://www.google.com/';
$api_call = "https://www.screenshot-website.com/api/$API_KEY?url=$url&type=tablet";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $api_call,
CURLOPT_TIMEOUT => 30,
CURLOPT_TIMEOUT_MS => 30000,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_CONNECTTIMEOUT_MS => 30000 )
);
$response = json_decode(curl_exec($curl), true);
if($errno = curl_errno($curl)) {
$error_message = curl_strerror($errno);
echo "cURL error ({$errno}):\n {$error_message}";
}
curl_close($curl); ?>
<img src="<?php echo $response['image']; ?>">
I get no errors at all.
I think your problem is with the "https" since curl set ssl_verifier to true
"The problem is that cURL has not been configured to trust the server’s HTTPS certificate." you can read the full information here.
You could try setting this option:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
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.