Php ошибка 400

<?php
$url ="http://absolutesms.com/Sendsms.aspx?userid=userid&password=password&clientid=clientid&senderid=absolute&mobilenumber=919000024365&smsmessage=SingleMessage".$request;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
echo $curl_scraped_page;
?>

After running this code I am getting HTTP Error 400. The request is badly formed. What should I do? I tried other url they are working fine the only problem is with this.
If I copy this url in browser it’s working but it’s giving error 400 when I put it and run it in curl.

Lee Taylor's user avatar

Lee Taylor

7,76116 gold badges34 silver badges49 bronze badges

asked Oct 13, 2012 at 13:27

user1460815's user avatar

0

I see this a lot. Usually fixed by running the request through urlencode().

answered Jul 14, 2013 at 21:19

Jay Julian Payne's user avatar

Try building URL with http_build_query as below

$url = 'http://absolutesms.com/Sendsms.aspx?' . http_build_query(array(
    'userid' => $userid,
    'clientid' => $clientid,
///... continue
));

The problem is probably due to unescaped characters that have special meanings in URLs.

http_build_query escapes them for you safely.

answered Oct 13, 2012 at 13:37

GBD's user avatar

GBDGBD

15.8k2 gold badges46 silver badges50 bronze badges

You are appending $request at the end of the assignment to $url but you don’t show us what $request contains.

Chances are that the value of $request tacked on the end that way is making the URL invalid.

I would expect to at least see a & at the end of the hardcoded URL string if you are going to append $request like that (although as @MichaelKrelin-hacker points out, this is unlikely to be the source of this specific error).

answered Oct 13, 2012 at 13:32

Trott's user avatar

TrottTrott

66.6k24 gold badges173 silver badges212 bronze badges

1

<?php
$url ="http://absolutesms.com/Sendsms.aspx?userid=userid&password=password&clientid=clientid&senderid=absolute&mobilenumber=919000024365&smsmessage=SingleMessage".$request;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
echo $curl_scraped_page;
?>

After running this code I am getting HTTP Error 400. The request is badly formed. What should I do? I tried other url they are working fine the only problem is with this.
If I copy this url in browser it’s working but it’s giving error 400 when I put it and run it in curl.

Lee Taylor's user avatar

Lee Taylor

7,76116 gold badges34 silver badges49 bronze badges

asked Oct 13, 2012 at 13:27

user1460815's user avatar

0

I see this a lot. Usually fixed by running the request through urlencode().

answered Jul 14, 2013 at 21:19

Jay Julian Payne's user avatar

Try building URL with http_build_query as below

$url = 'http://absolutesms.com/Sendsms.aspx?' . http_build_query(array(
    'userid' => $userid,
    'clientid' => $clientid,
///... continue
));

The problem is probably due to unescaped characters that have special meanings in URLs.

http_build_query escapes them for you safely.

answered Oct 13, 2012 at 13:37

GBD's user avatar

GBDGBD

15.8k2 gold badges46 silver badges50 bronze badges

You are appending $request at the end of the assignment to $url but you don’t show us what $request contains.

Chances are that the value of $request tacked on the end that way is making the URL invalid.

I would expect to at least see a & at the end of the hardcoded URL string if you are going to append $request like that (although as @MichaelKrelin-hacker points out, this is unlikely to be the source of this specific error).

answered Oct 13, 2012 at 13:32

Trott's user avatar

TrottTrott

66.6k24 gold badges173 silver badges212 bronze badges

1

The 400 Bad Request HTTP error code is a common issue that users can encounter when browsing the web. In this article, we will explore the causes of this error and provide you with some tips on how to troubleshoot and fix it. By the end of this post, you should have a better understanding of what causes the 400 Bad Request error and how to resolve it.

Table of Contents

  • What Is The 400 Bad Request Error?
  • Different 400 Error Messages
  • 400 Bad Request Causes
    • 1. 400 Error In The URL String Syntax
    • 2. 400 Error In the Request Headers
    • 3. 400 Error In the Cookies
    • 4. 400 Error In the request Body content
  • How To Fix The 400 Bad Request Error Code
    • 1. Verify the URL
    • 2. Clear the Session Storage
    • 3. When Uploading a file, check the size
    • 4. Inspect the logs
  • Summary
  • All HTTP Status Codes

What Is The 400 Bad Request Error?

This error occurs on your browser or “client side” when something is wrong with your request that the server cannot process.

As mentioned in the RFC 9110 HTTP Semantics by the IETF HTTP Working Group, the 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

Different 400 Error Messages

Depending on the web server, or browser, you may find different descriptive messages for this error:

  • 400 Bad Request.
  • Bad Request.
  • This page isn’t working. If the problem continues, contact the site owner. HTTP ERROR 400
  • This page isn’t working at the moment. If the problem persists, contact the site owner. HTTP ERROR 400

HTTP 400 Bad Request Error Page in Chrome

Default php 400 error message displayed in a Chrome browser

The default 400 Bad Request Error displayed in OSX Safari looks like a blank page:

Safari Blank page on HTTP Error 400

Safari default 400 error message.

On Microsoft Edge, the Error will show this:

HTTP Error 400 Page in OSX Microsoft Edge

OSX Microsoft Edge, HTTP Error 400 message.

Some hosting servers can provide their own error page, like our Wetopi Load Balancer 400 Error:

Wetopi 400 Bad Request Error Page

Wetopi personalized 400 error page.

We help you throughout the process

It doesn’t matter if you have one WordPress site or 100, our team is ready to carefully migrate your websites for free.

When migrating a website, we manually monitor it and verify its security.

400 Bad Request Causes

As per its definition earlier, a 400 Bad Request Error occurs when the web server can’t process something on your end. But don’t let a 400 Bad Request Error ruin your browsing experience! Check out the most common reasons behind this error:

1. 400 Error In The URL String Syntax

A 400 Bad Request error can occur if the URL is typed incorrectly, contains malformed syntax, or includes illegal characters. It may happen by mistake if we manually type the address, in particular if the URL has encoded chars (the ones that start with the %)

For example, you may reproduce this error by typing a wrong encoded char at the end of an url. Try copy/paste this url https://wetopi.com/%%0/

In this URL, the double %%0 is a wrong encoded char!

For example, one of the HTTP headers is malformed, or its size exceeds the maximum limit allowed by the server.

An HTTP header contains information about the request, such as the browser type, the URL, and the type of data sent. If the header is incorrect or too large, the server won’t be able to process the request and will return a 400 error code.

3. 400 Error In the Cookies

Cookies are a way for websites to store data on a user computer to remember their preferences, login information, and other details. However, like HTTP headers, cookies can also cause a “400 bad request” error if they are formatted incorrectly or exceed the server size limits. Some examples are:

  • If a website sets a cookie with a name that contains spaces or special characters, it may cause a “400 bad request” error if the cookie is not properly encoded or formatted. For example, a cookie with the name “my cookie” should be encoded as “my%20cookie” to be valid.
  • Another example of a malformed cookie is when it contains invalid characters or values. For instance, if a cookie value contains a semicolon or a comma, it can confuse the server cookie parsing algorithm and cause a “400 bad request” error.
  • Finally, cookies can exceed the server size limits, causing a “400 bad request” error.

4. 400 Error In the request Body content

When submitting content to a website, users may encounter a “400 Bad Request” error if the data sent to the server has an error or exceeds a limit. For example, if a user tries to upload a file to a website, and it exceeds the server file size limit, the server will reject the request and return the “400 Bad Request” error.

Similarly, if the user enters invalid or incomplete data in a form, the server may also reject the request and return the error message.

Need guidance?
In front of a demanding project?
Read this post about the top five criteria to select a hosting when you are a business

Wetopi

How To Fix The 400 Bad Request Error Code

Be sure to follow the next steps to fix the 400 error:

1. Verify the URL

The most common cause of a 400 Bad Request error is a mistake in the URL entered in the browser address bar. It could be due to typos, missing or incorrect characters, or an outdated or invalid URL. Double-check the URL, spelling, and formatting, especially if the address has encoded chars.

If the error persists, the next step is to clear the session storage and cookies in relation to the domain you are browsing. Keep reading:

2. Clear the Session Storage

When we remove all the Session Storage information for the page showing the error, we remove cookies, cache, and locally stored information.

How to clear the Session Storage on a Chromium-based (Google Chrome, Microsoft Edge, or any other browser that uses the Chromium engine):

  1. Open the browser and click the three dots icon in the top-right corner.
  2. Click “More tools” and then “Developer tools” or use the keyboard shortcut “Ctrl+Shift+I” (Windows) or “Cmd+Option+I” (Mac).

    Open the developer tools

  3. Select the “Application” tab and then click “Storage” in the left-hand panel:

    Developer Tools, clear site data

  4. In the right-hand panel click the “Clear site data” button.

At that stage, just reload the page and see if things are working again.

3. When Uploading a file, check the size

If you’re experiencing the 400 Bad Request Error while uploading a file, as a test, try using a much smaller file to see if the error goes away.

If this previous test works, then you know that the 400 Error is related to the Upload File Size limits of the server.

4. Inspect the logs

If the previous fixes do not solve the problem and you encounter the “400 Bad Request” error on your website, it could indicate a general problem with the server.

You can inspect the server logs for more information. Server logs should be accessible for clients in any hosting company, although sometimes they may be hard to locate or access.

If your WordPress site is hosted on a Wetopi server, you can easily access the logs with just two clicks.

  1. Open the Options Menu
  2. Click Show logs:

    Show Logs in Options Menu on wetopi server

Summary

The 400 Bad Request HTTP error code occurs when there is something wrong with the request made by the client side that the server cannot process.

This could be due to various causes:

  • an error in the URL string syntax
  • an error in the header request, cookies, or body content request.

In this article, we have suggested ways to fix this error by:

  • verifying the URL entered,
  • clearing the session storage,
  • or inspecting the logs.

We are techies passionate about WordPress. With wetopi, a Managed WordPress Hosting, we want to minimize the friction that every professional faces when working and hosting WordPress projects.

Not a wetopi user?

Free full performance servers for your development and test.
No credit card required.

All HTTP Status Codes

200 OK

201 Created

202 Accepted

203 Non-Authoritative Information

204 No Content

205 Reset Content

206 Partial Content

207 Multi-Status

208 Already Reported

226 IM Used

300 Multiple Choices

301 Moved Permanently

302 Found

303 See Other

304 Not Modified

305 Use Proxy

307 Temporary Redirect

308 Permanent Redirect

400 Bad Request

401 Unauthorized

402 Payment Required

403 Forbidden

404 Not Found

405 Method Not Allowed

406 Not Acceptable

407 Proxy Authentication Required

408 Request Timeout

409 Conflict

410 Gone

411 Length Required

412 Precondition Failed

413 Payload Too Large

414 Request-URI Too Long

415 Unsupported Media Type

416 Requested Range Not Satisfiable

417 Expectation Failed

418 I’m A Teapot

421 Misdirected Request

422 Unprocessable Entity

423 Locked

424 Failed Dependency

426 Upgrade Required

428 Precondition Required

429 Too Many Requests

431 Request Header Fields Too Large

444 Connection Closed Without Response

451 Unavailable For Legal Reasons

499 Client Closed Request

We are techies passionate about WordPress. With wetopi, a Managed WordPress Hosting, we want to minimize the friction that every professional faces when working and hosting WordPress projects.

Not a wetopi user?

Free full performance servers for your development and test.
No credit card required.

Are you trying to fix the 400 bad request error on WordPress? You are in the right place!

In this article, I’ll show you how to troubleshoot this common WordPress error.

Without further ado, let’s get started!

What is a 400 Bad Request Error?

The 400 Bad Request Error indicates that the server was unable to process the request due to invalid syntax. It falls into the Client Error category. So, the 400 Bad Request error happens before it is even processed by the server.

In this article, I’ll show you how to quickly fix this common WordPress error.

Let’s get started!

400 http error

Main causes of 400 Bad Request Errors on WordPress

Errors 400 can have a multitude of reasons, but here are the most common ones:

Uploading a too big file

To protect bandwidth, many web servers (almost all of them) limit by default the size of the files your visitors can upload. When such an upload occurs, the server returns a 400 Bad Request error. Anyway, it’s best for security not to allow your user to upload heavy files on WordPress websites.

Sending a request to an invalid URL

Sending a request to an invalid URL (ie: with erroneous characters, malformed syntax, or illegal characters) can return a 400 Bad Request Error. This can happen so easily if you don’t encode your URL correctly.

Expired/wrong cookie

Sometimes your browser can store invalid or expired cookies. The server may perceive this cookie as dangerous and return the 400 Bad Request Error code.

Elementor

Sometimes Elementor users encounter the 400 error. Don’t worry, the solutions we propose in this article also work for Elementor users.

Troubleshooting the 400 Bad Request Error Code on WordPress

Nota bene: Before trying to fix any HTTP error on WordPress, you should ALWAYS make a backup of your WordPress Website (FTP + Database).

Although it’s categorized as a client error, the 400 bad request error code is often at a crossroad between, the client, the server, and/or a web application. Determining the cause and fixing the 400 bad request error code on WordPress can thus be challenging.

This being said, the 400 bad request error designates that the request sent by the client was invalid for some reason, so here is what you should do first if you are using WordPress: Make a backup!!! :-)

Check the Solicited URL

Wrong URL strings are the most common reasons for a 400 Bad Request error. Check that there are no unwanted or illegal characters in the URL you typed in your browser. You also need to make sure they’re separated with adequate slashes.

Sometimes, the URL has special characters such as %%%, which can lead to the 400 error. If this happens, you have to check that your WordPress website is encoded properly.

If you struggle with this, check out this amazing free URL Encoder & Decoder Tool for WordPress. When you think that you have a good URL structure, try to refresh the page. If it doesn’t work, go to the next step!

Clear your Web Browser Cache and Cookies

You may be surprised, but most 400 errors in WordPress can be fixed by clearing your browser’s cache and cookies.

Browser caches temporarily store images, scripts, and other parts of websites you visit to speed up your browsing experience. Some of the data stored can be expired and thus responsible for the 400 Bad Request Error. Clearing your browser cache and cookies might troubleshoot the 400 HTTP error code.

Clear Cache & Cookies Chrome

In Chrome, enter this address in your browser’s URL field: chrome://settings/clearBrowserData

You will directly access the clear browsing data dashboard.

Clear google chrome cache

Here, make sure that the “cached images and files“, and “cookies” boxes are ticked. Then click on the Clear data button. Refresh your page, if it doesn’t work, go to the next step!

Nota bene: On a Mac, you can refresh a web page and automatically clear its cache with the shortcut: SHIFT+CMD+R

Clear Cache & Cookies Mozilla

Click the menu button Fx57Menu and select “Preferences” > Privacy & Security panel. You will lend here:

clear cache firefoxe

Tick the “Cookies and Site Data” and “Cached Web Content” boxes and click “clear data”

If you are not using Google Chrome or Firefox, check out these guides (I don’t have IE, Edge or Safari on my laptop):

  • For Internet Explorer
  • For Microsoft Edge
  • For Safari

Log Out of your WordPress Account and Log In Again

If you have emptied your browser cache and cookies, you have automatically been logged out of WordPress. Try to log in again.

WordPress admin panel

By doing so, you will refresh your credential and maybe get rid of the 400 Bad Request Error Code. If it doesn’t work, let’s go to the next step!

Diminish the size of the file you tried to upload

Many servers limit the size of files for security reasons. If you’re facing the bad request error while uploading a file, try again with a much smaller file to see if this resolves the 400 error.

How to reduce file size?

  1. To reduce the image size, you can use a compression plugin like ShortPixel or a third-party application like CompressJPEG.
  2. To reduce video size on WordPress, check out this great guide from Digi Effects.

Clear your cache in WordPress

It’s highly unlikely, but the 400 Bad Request Error can also be triggered by your cache plugin. You might want to purge your cache to troubleshoot this error.

To clear the cache on WP Rocket you need to click here:

Clear WP Rocket cache

To purge the cache on WP Super Cache you need to click there:

Clear WP Super Cache

Remove the last plugins you installed

Plugins and themes can enhance your WordPress website functionalities, but they can also generate PHP errors and trigger the 400 Bad Request Error Code.

Monitoring WordPress PHP error logs is a good way to detect the plugins that can slow and jeopardize your website. Removing one by one the last plugins you installed, might be a good way to fix the 400 bad request error.

If you want to see which plugins generate PHP errors look at WP Umbrella.

Downgrade your WordPress version

If you have updated WordPress just before the 400 bad request error showed up – and if none of the solutions we suggested you before worked – you may want to consider rolling back to the previous WordPress version you had installed when things were working just fine.

If you don’t know how to downgrade your WebPress website, check out this article from Kinsta!

Final thoughts on the 400 bad request error code on WordPress

In most cases, the 400 Bad Request error is coming from the client side and is thus easy to fix. However, from time to time, this error can shed light on more problematic generic server issues.

If none of our tips and fixes worked, you should suspect the bad request error to be a server side issue. In such a situation, you have to contact your web host provider.

You may also want to see my article on how to fix the 500 internal server error on WordPress.

While I’m trying to send a GET request to an address with php I receive HTTP 400 BAD REQUEST message and I can’t figure out why.

Here’s my code:

function redirect($url)
{
    error_reporting(E_ERROR | E_PARSE);

    $components = parse_url($url);

    $port = $components["port"];
    $ip = $components["host"];
    $path = $components["path"];

    //create and connect socket with the parameters entered by the user
    //$sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);

    echo "Establishing connection to the given adress...\n";


    //Connection timeout limit is set to 10 seconds...
    if(!isset($port))
    {
        $port = 80;
    }
    $sock = fsockopen($ip, $port,$errno, $errstr, 10) or die("Unable to connect...");

    $request = "GET $path HTTP/1.1" . "\r\n\r\n";


    fwrite($sock, $request);

    while ($header = stream_get_line($sock, 1024, "\r\n")) {
        $response.= $header . "\n";
    }
    echo $response;
    $loc = "";

    if (preg_match("/Location:\'(.*)\\n/", $response, $results))
        $loc = $results[1];

    echo $loc;

}

Any suggestions?

Понравилась статья? Поделить с друзьями:

Интересное по теме:

  • Php отследить ошибку
  • Php отправить код ошибки
  • Php отобразить все ошибки
  • Php отображать все ошибки
  • Php отловить 500 ошибку

  • Добавить комментарий

    ;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: