0

I've got a few URL's that are being run through php curl so that I can get the HTTP Code. The funny thing is, some URL's work fine and return 200, but others always return 400. The problem only exist when I feed the URL from the database, if I run it by hardcoding it then it returns 200 as it should.

I've tried urlencoding/decoding and it's not the issue. It looks fine in appearances. The one odd thing that I've been able to find is that the 400 URL's always have a [content_type] => text/html; charset=iso-8859-1. The url's are also really simple https://sometext.something.something/test and don't have any parameters

Here's the PHP code i've been running the URL through. It works on my localhost just fine, but on the server it causes issues.

function checkStatus($url) {
    $urlencode=urlencode($url);
    foreach (explode('&', $urlencode) as $chunk) {
        $param = explode("=", $chunk);

        if ($param) {
            echo urldecode($param[0]);
            $ch = curl_init(urldecode($param[0]));
        }
    }

    //$ch = curl_init($urlencode);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    // curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    // curl_setopt($ch, CURLINFO_CONTENT_TYPE, true);
    // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:text/html;charset=UTF-8'));
    curl_exec($ch);
    $returnCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $error_report= curl_getinfo($ch);
    print_r($error_report);
    curl_close($ch);

    return $returnCode;
}

Here's a sample of the error message:

Array
(
    [url] => https://url.net/something
    [content_type] => text/html; charset=iso-8859-1
    [http_code] => 400
    [header_size] => 171
    [request_size] => 83
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.056926
    [namelookup_time] => 0.003457
    [connect_time] => 0.004276
    [pretransfer_time] => 0.01614
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => 0
    [starttransfer_time] => 0.056906
    [redirect_time] => 0
    [certinfo] => Array
        (
        )

)
2
  • Have you tried using parse_url instead of urlencode / urldecode? Commented Mar 26, 2014 at 18:28
  • @Arkadiusz'flies'Rzadkowolski No I haven't. Testing now. EDIT: Well, I'm not really sure how it helps here. It returns the url in the expected format but not much else. Commented Mar 26, 2014 at 18:31

1 Answer 1

1

Your URL has https:// that's why it doesn't work for you.

If the data security is not a problem for you, then you can use the following option to use insecure communication for ssl.

 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
Sign up to request clarification or add additional context in comments.

1 Comment

YES! It worked! Although while messing around with the code, I noticed that if I changed the query to only output 1 url (limit 1) it would return 200 even though it's the same url that returns 400 when select *

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.