Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get website using cURL

I am not able to fetch data using cURL in php AS I can do this using curl command in terminal like below -

curl http://www.universalapplianceparts.com/search.aspx?find=w10130694

This command provide expected result in terminal.

I want same result in php using curl

Below is my php code -

$url = "http://www.universalapplianceparts.com/search.aspx?find=W10130694";
$ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL,"http://www.universalapplianceparts.com/");
    curl_setopt($ch, CURLOPT_POST, true);  
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $output = curl_exec ($ch); 
    curl_close ($ch); 
    var_dump($output);

Please let me know why I am not getting same result using php curl as I am getting in terminal.

Thanks

like image 732
John Avatar asked Nov 15 '25 16:11

John


1 Answers

Try this ::

    $url = "http://www.universalapplianceparts.com/search.aspx?find=W10130694";
$ch1= curl_init();
curl_setopt ($ch1, CURLOPT_URL, $url );
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1,CURLOPT_VERBOSE,1);
curl_setopt($ch1, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)');
curl_setopt ($ch1, CURLOPT_REFERER,'http://www.google.com');  //just a fake referer
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1,CURLOPT_POST,0);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, 20);

$htmlContent= curl_exec($ch1);
echo $htmlContent;
like image 86
Exception Avatar answered Nov 18 '25 06:11

Exception



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!