Bynd
Bynd

Reputation: 705

php curl return 400 for existing URL

I create a function to verify if url exist or not, it's working except for some urls like from soundcloud, urls from soundcloud return 400 but the pages exist I have no space or any special character in the url my code is

$curl = curl_init($page_url);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
        $result = curl_exec($curl);
        $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

var_dump ($result) //return true

var_dump ($statusCode) // return int(400)

I tried using

`CURLOPT_RETURNTRANSFER, TRUE`
`CURLOPT_FOLLOWLOCATION, TRUE`

but is the same result var_dump ($statusCode) // return int(400)

Upvotes: 0

Views: 120

Answers (1)

Tarek
Tarek

Reputation: 140

Technically the status code 400 (Bad Request) doesn’t mean that the page doesn’t exist, It means that the website/API that you’re trying to visit was expecting some input from the user/developer to be in a specific format which the user that’s visiting the website failed to supply.

Since you are trying to CURL a website ( and not an API ) I’d suggest that you open the website with a web browser , and check for any cookies that the website require to function properly and include those cookies in your CURL requests.

EDIT:

To Include cookies to your CURL you use the CURLOPT_COOKIE CURL Option :

 curl_setopt($curl, CURLOPT_COOKIE, 'user=ellen; activity=swimming');

And keep in mind that not all pages are meant to be browsed anonymously, some web pages might require that you log in to view them.

Upvotes: 1

Related Questions