'PHP curl via curl_exec() different than console curl
Thanks for your help in the past, guys. Learning 'curl' and have read docs, help topics, and tutorials but can't seem to curl using php for this site. I have a method that includes doing a lot of curl_setopt()'s and it works for another site, but not for this one. So I have gone back to the drawing board and have decided to start simple and just do a basic curl with proxy
$randomProxy = http://password:[email protected]:31280;
$searchURL = sprintf("https://www.bestbuy.com/site/searchpage.jsp?st=%s",$focusSKU);
$curlBestBuy = 'curl --proxy "'.$randomProxy.'" "'.$searchURL.'"';
$searchPageText = curl_exec($curlBestBuy);
$curl_error = curl_error($searchPageText);
Where $curlBestBuy results in:
curl --proxy "http:/password:[email protected]:31280" "https://www.bestbuy.com/site/searchpage.jsp?st=145131"
When the php is run it errors to: 'curl_exec() expects parameter 1 to be resource, string given'.
But, if I copy the curl statement on use it in a Ubunto console, it at least gives me a response from the site saying access denied.
Why two different responses from what should be the same thing (curl_exec and unbuntu console)?
There some parameter I am not settting? Is this to basic? Being basic, I will build it stronger later to avoid being blocked, but shouldn't the php version at least give me an 'access denied' like when I paste it into the console?
Solution 1:[1]
Here is an example howto use PHP curl functions. This is better than run curl binary with shell_exec()
$url = 'https://www.bestbuy.com/site/searchpage.jsp?st=...';
$ch = curl_init(); // start curl session
curl_setopt($ch, CURLOPT_URL, $url); // set URL
curl_setopt($ch, CURLOPT_PROXY, 'us.proxymesh.com:31280'); // proxy address
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'proxyuser:password'); // proxy-auth
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // follow location
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // curl_exec() will return string
$string = curl_exec($ch); // start curl request
if($string === FALSE) { // if request goes wrong it will return FALSE
$error = curl_error($ch); // return error from this session
}
curl_close($ch); // close curl session
echo $string;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | daniel |
