I am trying to do some curl'ing from PHP
I have:
$url="http://sites.target.com/site/en/spot/mobile_fiats_results.jsp?_DARGS=/site/en/spot/mobile_fiats.jsp&asin=&dpci=".$arriPadSKU[$counter]."&zipcode=".$vZip."&city=&state=";
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL, '$url');
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
$url produces a URL like this and it works in a browser:
http://sites.target.com/site/en/spot/mobile_fiats_results.jsp?_DARGS=/site/en/spot/mobile_fiats.jsp&asin=&dpci=057-10-1830&zipcode=95128&city=&state=
However I dont seem to get results from my Curl c开发者_如何学编程all, I dont think any results are returned. If I look though the results in $buffer I dont see the data like I do on the page.
Can I not set a $url like this to CURLOPT_URL?
How would I structure this From a command line this works:
echo "iPad 2 Wi-Fi White 16GB: "; curl -s --data "_dyncharset=ISO-8859-1&asin=&dpci=057-10-1839&zipcode=95128&city=&state=" http://sites.target.com/site/en/spot/mobile_fiats_results.jsp?_DARGS=/site/en/spot/mobile_fiats.jsp
The problem is you're enclosing $url in string literal 's.
If you run curl_getinfo($curl_handle); you'll see the url it's requesting is http://$url which is obviously not valid.
Array
(
[url] => http://$url
[content_type] =>
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0
[namelookup_time] => 0
[connect_time] => 0
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0
[redirect_time] => 0
[certinfo] => Array
(
)
)
EDIT
Here is the updated code I'm using to test:
<?php
$url="http://sites.target.com/site/en/spot/mobile_fiats_results.jsp?_DARGS=/site/en/spot/mobile_fiats.jsp&asin=&dpci=057-10-1830&zipcode=95128&city=&state=";
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL, $url);
//curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
//curl_close($curl_handle);
print $buffer."\n";
print_r(curl_getinfo($curl_handle));
?>
This works for me. However, the only other problem I see that could prevent you from receiving data back is your insanely low connection timeout. Try setting it higher.
Again, the above code does work.
UPDATED AGAIN
The reason you're recieving an error back is because the target site checks for a valid user agent.
Something like:
curl_setopt($curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.15) Gecko/20110303 Ubuntu/10.04 (lucid) Firefox/3.6.15");
works just fine.
Try
curl_setopt($curl_handle,CURLOPT_URL, $url);
instead of
curl_setopt($curl_handle,CURLOPT_URL, '$url');
精彩评论