phpcurl数据 php curl_setopt

php curl 取不到数据 帮我看下 是为什么有以下几种可能:
1、服务器端确实没有数据返回;
2、curl写错phpcurl数据了;
3、试试下面这个phpcurl数据,phpcurl数据我在用的
function curl($url,$post = 'POST',$data = https://www.04ip.com/post/array()){
$ch = curl_init();
$headers[] = "Accept-Charset: utf-8";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $post);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result,1);
}
PHP curl 模拟表单数据流multipart/form-data上传文件在调用公众号接口".$token."type=".$type;
上传永久素材文件总是返回 "{\"errcode\":41005,\"errmsg\":\"media data missing\"}"
经过多次测试使用下面的方式,可以正常上传
//调用测试
protected static $url;
protected static $delimiter;
protected static $instance;
public function index()
{
static::$delimiter = uniqid();
$basename = Request::instance()-root();
if (pathinfo($basename, PATHINFO_EXTENSION) == 'php') {
$basename = dirname($basename);
}
$result=$this-wxAddMaterial($token,$basename.'/upload/images/gnlog.jpg','image');
}
// 新增其他类型永久素材
public function wxAddMaterial($token,$filename='',$type='') {
// 设置请求参数
static::$url = "".$token."type=".$type;
$filePath = str_replace('\\', '/', $filename);
// 发送请求
$imginfo=pathinfo($filePath);
$fields = array(
'media'=file_get_contents(".".$filePath),
'filename'=$imginfo["basename"],
);
$res = $this-putPart( $fields);
// 发送请求
return $res;
}
//推送文件流
public function putPart($param) {
$post_data = https://www.04ip.com/post/static::buildData($param);
$curl = curl_init(static::$url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
"Content-Type: multipart/form-data; boundary=" . static::$delimiter,
"Content-Length: " . strlen($post_data)
]);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
//编译请求头格式和数据流
private static function buildData($param){
$datahttps://www.04ip.com/post/= '';
$eol = "\r\n";
$upload = $param['media'];
unset($param['media']);
foreach ($param as $name = $content) {
$data .= "--" . static::$delimiter . "\r\n"
. 'Content-Disposition: form-data; name="' . $name . "\"\r\n\r\n"
. $content . "\r\n";
}
$data .= "--" . static::$delimiter . $eol
. 'Content-Disposition: form-data; name="media"; filename="' . $param['filename'] . '"' . "\r\n"
. 'Content-Type:application/octet-stream'."\r\n\r\n";
$data .= $upload . "\r\n";
$data .= "--" . static::$delimiter . "--\r\n";
return $data;
}
根据自己的实际情况稍作修改
解析php中curlphp的multi_curl功能慎用 因为某些版本的curl和php的搭配有Bug 所以phpcurl数据你调试过没问题的代码很可能在别的机器上不正确
相信许多人对php手册中语焉不详的curl_multi一族的函数头疼不已 它们文档少 给的例子 更是简单的让你无从借鉴 我也曾经找phpcurl数据了许多网页 都没见一个完整的应用例子 ?curl_multi_add_handle ?curl_multi_close ?curl_multi_exec ?curl_multi_getcontent ?curl_multi_info_read ?curl_multi_init ?curl_multi_remove_handle ?curl_multi_select 一般来说 想到要用这些函数时 目的显然应该是要同时请求多个url 而不是一个一个依次请求 否则不如自己循环去调curl_exec好了

推荐阅读