Jump to content

PHP - cURL or something like it

Go to solution Solved by Mr_KoKa,
// Include header in result? (0 = yes, 1 = no)

I think it is other way around 1 is enabled and 0 is disabled, other then that you're good. Do you have any particular problem with curl implementation?

 

My plain curl request looks like this:

GET / HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: curl/7.52.1
Accept: */*

And request made using your curl code results in such request:

GET / HTTP/1.1
User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10
Host: 127.0.0.1:8000
Accept: */*

So the User-Agent header is set.

 

So what is not working for you? if there is no output and you know thet crul_exec return is === FALSE  then you can try to curl_error on curl handle $ch to know what is wrong.

Hi, trying to use curl to get a page and send headers, but it doesent work, tho I managed to get this file_get_content to work, but I want curl so it will be easyer to deal with errors.

 

This is the file_get_contents

$url = "someurl";

$options = array(
    'http'=>array(
        'method'=>"GET",
        'header'=>"User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n" // i.e. An iPad
    )
);

$context = stream_context_create($options);
$file = file_get_contents($url, false, $context);

echo $file;

and the cURL

function curl_download($Url){

    // is cURL installed yet?
    if (!function_exists('curl_init')){
        die('Sorry cURL is not installed!');
    }

    // OK cool - then let's create a new cURL resource handle
    $ch = curl_init();

    // Now set some options (most are optional)

    // Set URL to download
    curl_setopt($ch, CURLOPT_URL, $Url);

    // User agent
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10");

    // Include header in result? (0 = yes, 1 = no)
    curl_setopt($ch, CURLOPT_HEADER, 0);

    // Should cURL return or print out the data? (true = return, false = print)
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Timeout in seconds
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);

    // Download the given URL, and return output
    $output = curl_exec($ch);

    if (!$output) {
        $output = 'Error';
    }

    // Close the cURL resource, and free system resources
    curl_close($ch);

    return $output;
}

echo curl_download('someurl');

 

Back-end developer, electronics "hacker"

Link to comment
https://linustechtips.com/topic/736514-php-curl-or-something-like-it/
Share on other sites

Link to post
Share on other sites

// Include header in result? (0 = yes, 1 = no)

I think it is other way around 1 is enabled and 0 is disabled, other then that you're good. Do you have any particular problem with curl implementation?

 

My plain curl request looks like this:

GET / HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: curl/7.52.1
Accept: */*

And request made using your curl code results in such request:

GET / HTTP/1.1
User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10
Host: 127.0.0.1:8000
Accept: */*

So the User-Agent header is set.

 

So what is not working for you? if there is no output and you know thet crul_exec return is === FALSE  then you can try to curl_error on curl handle $ch to know what is wrong.

Link to post
Share on other sites

20 hours ago, Mr_KoKa said:

// Include header in result? (0 = yes, 1 = no)

I think it is other way around 1 is enabled and 0 is disabled, other then that you're good. Do you have any particular problem with curl implementation?

 

My plain curl request looks like this:


GET / HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: curl/7.52.1
Accept: */*

And request made using your curl code results in such request:


GET / HTTP/1.1
User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10
Host: 127.0.0.1:8000
Accept: */*

So the User-Agent header is set.

 

So what is not working for you? if there is no output and you know thet crul_exec return is === FALSE  then you can try to curl_error on curl handle $ch to know what is wrong.

Yes you are correct about that 0 for no and 1 for yes in header results, and I got it working, just needed to install SSL stuff on the windows machine so I ignored that part and moved it to the server and it works there, so Yes it works :) Thanks.

Back-end developer, electronics "hacker"

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×