curl_init($ url)返回资源ID#7

时间:2012-05-14 02:58:41

标签: php

我一直使用CURL功能从网址获取图片并将其保存到m服务器,因为我的托管已禁用COPY()。

我通过的网址是$ url = http://upload.wikimedia.org/wikipedia/commons/thumb/e/e8/People_from_Switzerland.png/285px-People_from_Switzerland.png

    $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';              
$headers[] = 'Connection: Keep-Alive';         
$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';   
$userAgent = 'php';
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);         
curl_setopt($process, CURLOPT_HEADER, 0);         
curl_setopt($process, CURLOPT_USERAGENT, $useragent);         
curl_setopt($process, CURLOPT_TIMEOUT, 30);         
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);         
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

当我尝试回显curl_init($ url)输出时出现以下错误

流程资源ID#7
警告:curl_setopt()期望参数1为资源,在中给出null 警告:curl_errno()期望参数1为资源,在

中给出null

请帮助!

3 个答案:

答案 0 :(得分:4)

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$ch来自哪里,之前使用$process,是从其他地方复制的最后一行?

未定义的变量将为null。

答案 1 :(得分:3)

您忘了在最后一行中将$ch更改为$process

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

应该是:

curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);

答案 2 :(得分:0)

使用PHP从您提供的任何网址下载图像文件并保存。我把它用于脸书。

$headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';  
$headers[] = 'Connection: Keep-Alive';
$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';

$userAgent = 'php';
$process = curl_init('https://sphotos-a.xx.fbcdn.net/xxxx/xxxxxx_n.jpg');
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
curl_setopt($process, CURLOPT_HEADER, 0);     
curl_setopt($process, CURLOPT_USERAGENT, $useragent);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1)
curl_setopt($process, CURLOPT_BINARYTRANSFER,1);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
$raw=curl_exec($process);
curl_close ($process);
$open=fopen('final.jpeg',x);
$write=fwrite($open,$raw);
$clo= fclose($open);