With curl
from linux bash I can download a webpages passing some variables
curl --data "var1=val1&var2=val2&var3=&var4=val4&btnSubmit=btnName" https://<url> -o "<fileToSave>"
I need to do the same things but to get the full html code of that page into a php variable. I'm trying with this:
<?php $ch = curl_init();
$post_data = array (
"var1" => "val1",
"var2" => "val2",
"var3" => "",
"var4" => "val4",
"btnSubmit" => "btnName"
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$response = curl_exec($ch);
?>
The problem is that from bash I can retrieve the html pages and save it with this code my $response
value is empty.
What's wrong?
Use http_build_query()
over the $post_data
, otherwise curl will assume to POST
multipart/form-data
posting based on the Array
Data type(that's why it is not working for you).
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
http_build_query()
converts the array into key1=value1&key2=value2
formated string with automatic urlencode()
.
Seems like you are connecting to a HTTPs URL. You should enable this cURL
parameter.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.