file_get_contents throws 400 Bad Request error PHP - php

I'm just using a file_get_contents() to get the latest tweets from a user like this:
$tweet = json_decode(file_get_contents('http://api.twitter.com/1/statuses/user_timeline/User.json'));
This works fine on my localhost but when I upload it to my server it throws this error:
Warning: file_get_contents(http://api.twitter.com/1/statuses/user_timeline/User.json) [function.file-get-contents]:failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request...
Not sure what might be causing it, maybe a php configuration I need to set on my server?
Thanks in advance!

You might want to try using curl to retrieve the data instead of file_get_contents. curl has better support for error handling:
// make request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.twitter.com/1/statuses/user_timeline/User.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
// convert response
$output = json_decode($output);
// handle error; error output
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
var_dump($output);
}
curl_close($ch);
This may give you a better idea why you're receiving the error. A common error is hitting the rate limit on your server.

You can use file_get_contents adding the ignore_errors option set to true, in this way you will get the entire body of the response in case of error (HTTP/1.1 400, for example) and not only a simple false.
You can see an example here: https://stackoverflow.com/a/11479968/3926617
If you want access to response's headers, you can use $http_response_header after the request.
http://php.net/manual/en/reserved.variables.httpresponseheader.php

Just a little addendum on Ben's answer.
According to the PHP manual the CURLOPT_URL option may be set when inizializing the cURL handle with curl_init().
// make request
$ch = curl_init("http://api.twitter.com/1/statuses/user_timeline/User.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
// convert response
$output = json_decode($output);
// handle error; error output
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
var_dump($output);
}
curl_close($ch);

Related

How To Bypass Cloudfare using php or javascript

My problem is
I have made curl request on paxfull api earlier it was returning result but now its returning 503 .
Here is my code
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, 'https://paxful.com/buy-bitcoin?format=json');
curl_setopt($handle, CURLOPT_POST, false);
curl_setopt($handle, CURLOPT_BINARYTRANSFER, false);
curl_setopt($handle, CURLOPT_HEADER, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 10);
$response = curl_exec($handle);
$hlength = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
$body = substr($response, $hlength);
// If HTTP response is not 200, throw exception
if ($httpCode != 200) {
throw new Exception($httpCode);
}
I get this error:
Error Fatal error: Uncaught exception 'Exception' with message '503'
I googled and found it might be ip address blocked but when made get request at browser its giving results to me.
now i came with conclusion they are not allowing any GET Request .
if you run url https://paxful.com/buy-bitcoin?format=json it first check browser the return the result.
how can we get results of paxfull api. please suggest
here is snapshot
then there api redirects 404
url http://localhost/cdn-cgi/l/chk_jschl?jschl_vc=c5b74eae14eb1b1e5862f913b9f0f178&pass=1499953121.017-h%2FljgkjMr%2B&jschl_answer=18913
Its not possible through javascript also
fiddle http://jsfiddle.net/00cvyyuo/350/
i found link How to bypass cloudflare bot/ddos protection in Scrapy?
but this link helps in python so can someone help in php or javascript.
try to use Chrome's network tab in Developer Tools(f12) to see the actual request being sent. If it works then try to repeat the request from a REST client, where you can edit the headers to check what works and what not.
If you got it working then set all the headers in cURL. If it fails then set verbose and check what was sent.

File get content failed to open stream - Json response

For now i'm testing using file_get_content(url), but it's returning an error.
The request and response are in the same server:
warning: file_get_contents(https://192.168.1.15/adverts/locations): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in xxxxx
The Web-service returns a json object, so the function file_get_contents will receive a json.
I want to use cURL approach, but it doesn't work too.
So i thought to start ,fix this error first and get contents, and after try to debug why cURL is not working, since i'm getting content with this function
{"status":"success","alerts":[],"content":{"types":[{"id":"P","description":"Permanent"},{"id":"C","description":"Contract"}]}}
Try this with your webservice url and tell us if it throws some kind of Exception :
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "192.168.1.15/adverts/locations");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
Are you sure you can use HTTPS with your server ? Have you the right settings and certificates ?

Fast check files exists in other server

I try check is exists in other file, if file exists server response HTTP 200 if not HTTP 302.
now i try check with get_headers() function but it to slow, because files size about 2mb-10mb and i check about 20 file in a while and it spend about 5-10 sec.
Perhaps there are other options?
Try using curl with the CURLOPT_NOBODY option.
Example:
$ch = curl_init("http://www.other-server.com/file.jpg");
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$response_code will contain the HTTP response code.

Handle 403 error without making redirection

I wanted to handle 403 error without using server side error redirection methods(i.e. without using .htaccess file). Sometimes server returns a 403 error message (forbidden access). So is it possible to have PHP script which handles this 403 error message?
For example, Before showing error page, I would like to obtain server status when it my specific php page runs, and without making a redirection, I would just like to display custom message in the that page.
Some solutions for you.
Check for URL errors and make sure the actual web page is specified. Its common reason for a web site to return the 403 Forbidden error, when the URL is pointing to a directory instead of a web page. Which can be done using HttpRequest Class in PHP. You can use http_get to perform GET request. You can also Test URL here.
<?php
$response = http_get("URL", array("timeout"=>1), $info);
print_r($info);
?>
Output:
array (
'effective_url' => 'URL',
'response_code' => 403,
.
and so on
)
What is important for you is response_code with which you can play further.
Use of curl.
function http_response($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if(!$head)
{
return FALSE;
}
return $httpCode;
}
$errorcode = http_response("URL"); //if success 200 otherwise different

connecting to volusion store using curl functions

I have volusion store URL and I am connecting to volusion store using this URL and curl function. Following is my code to connect -
$URL
='http://v970032.y5pgm9yfhypo.demo17.volusion.com/net/WebService.aspx?Login=rashmi#edreamz.in&EncryptedPassword=24EA69124482A486AF3E6BA68DDEECBB7CBC3661EA792E0DC4A40CD6FC031E6E&Import=Update';
$ch = curl_init($URL);
I just want to check whether the connection for the given URL is connected or not. How can I achieve this please advise me. Right now if I used -
echo $ch
it is only displaying Resource id #2
thanks in advance.
First, curl_init ins't enough. You'll need to use curl_exec to actually send the request. Once the request is sent, you can use curl_errno to check if an error was returned and curl_getinfo to get more information about the request and response such as the HTTP response code (e.g.: 200, 404), how long it took to connect, etc.
Modified example from the curl_getinfo documentation:
<?php
// Create a curl handle
$ch = curl_init('http://v970032.y5pgm9yfhypo.demo17.volusion.com/net/WebService.aspx?Login=rashmi#edreamz.in&EncryptedPassword=24EA69124482A486AF3E6BA68DDEECBB7CBC3661EA792E0DC4A40CD6FC031E6E&Import=Update');
// Tell cURL to return the transfer instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute
$result = curl_exec($ch);
// Check if any error occured
if (!curl_errno($ch)) {
// Here, I'm outputting the value of $result but you can do whatever you'd like
// with it such as using a DOMDocument to parse XML or HTML, json_decode to
// parse JSON, etc.
echo $result;
} else {
echo 'Sorry, an error occurred while connecting to Volusion.';
}
// Close handle
curl_close($ch);
Depending on what you're trying to do, you might want to have a look at curl_setopt and more specifically at CURLOPT_RETURNTRANSFER and some of the other basic options you can pass to cURL. The options are well documented.

Categories