The problem of “Call to undefined function curl_init()” may occur when you use PHP’s curl development for the first time, which means that PHP has not configured the curl execution environment.
1. Confirm whether the semicolon before extension=php_curl.dll in php.ini has been removed. After removing it, restart the apache server.
2. If the same problem still exists, you need to copy the libeay32.dll and ssleay32.dll files in the php directory and the php_curl.dll in the ext directory to the C:WINDOWSsystem32 directory. If the above file does not exist, download it, copy it to the corresponding directory, and restart the apache server. (PS: It means that there has never been such a problem, and the solution was also found on Baidu)
After the environment is configured successfully, there may be a problem of no return value when calling the curl_exec method. First, confirm whether you are accessing https. When curl accesses the URL, SSL authentication is performed by default, so you need to add the following code
<code>curl_setopt(<span>$ch</span>, <span>CURLOPT_SSL_VERIFYPEER</span>, <span>false</span>); <span>//</span>不验证证书 curl_setopt(<span>$ch</span>, <span>CURLOPT_SSL_VERIFYHOST</span>, <span>false</span>); <span>//</span>不验证证书</code>
Attached below is the code for php to use curl to simulate post requests
<code><span><span>function</span><span>request_post</span><span>(<span>$url</span> = <span>''</span>, <span>$post_data</span> = array<span>()</span>)</span> {</span><span>if</span> (<span>empty</span>(<span>$url</span>) || <span>empty</span>(<span>$post_data</span>)) { <span>return</span><span>false</span>; } <span>$o</span> = <span>""</span>; <span>foreach</span> ( <span>$post_data</span><span>as</span><span>$k</span> => <span>$v</span> ) { <span>$o</span>.= <span>"$k="</span> . urlencode( <span>$v</span> ). <span>"&"</span> ; } <span>$post_data</span> = substr(<span>$o</span>,<span>0</span>,-<span>1</span>); <span>$postUrl</span> = <span>$url</span>; <span>$curlPost</span> = <span>$post_data</span>; <span>$ch</span> = curl_init();<span>//初始化curl</span> curl_setopt(<span>$ch</span>, CURLOPT_URL,<span>$postUrl</span>);<span>//抓取指定网页</span> curl_setopt(<span>$ch</span>, CURLOPT_POST, <span>1</span>);<span>//post提交方式</span> curl_setopt(<span>$ch</span>, CURLOPT_POSTFIELDS, <span>$curlPost</span>); curl_setopt(<span>$ch</span>, CURLOPT_SSL_VERIFYPEER, <span>false</span>); <span>//不验证证书</span> curl_setopt(<span>$ch</span>, CURLOPT_SSL_VERIFYHOST, <span>false</span>); <span>//不验证证书</span><span>$data</span>=curl_exec(<span>$ch</span>); curl_close(<span>$ch</span>); <span>return</span><span>$data</span>; }</code>
The above has introduced the use of curl, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.