Created
September 30, 2012 08:39
-
-
Save fushihara/3806269 to your computer and use it in GitHub Desktop.
CURLOPT_POSTFIELDS fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$postArr=array()l | |
$postArr["message"]="@user"; | |
$postArr["image"]="_PRE_"."/file.png"; | |
$post2=post2($postArr,"_PRE_"); | |
$curl = curl_init(); | |
$header=array(); | |
$header[]=$post2["header"]; | |
$post=$post2["post"]; | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_HTTPHEADER,$header); | |
curl_setopt($curl, CURLOPT_POSTFIELDS,$post); | |
$result = curl_exec($curl); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function post2($postArr,$prefix="@"){ | |
//return {post,header} | |
$return=array("post"=>"","header"=>""); | |
$bounbary=md5(microtime().rand()); | |
$return["header"]="Content-Type: multipart/form-data; boundary={$bounbary}"; | |
foreach($postArr as $key=>$value){ | |
if(strpos($value,$prefix)===0){ | |
//これはファイル名 | |
$filePath=substr($value,strlen($prefix)); | |
$baseName=basename($filePath); | |
$return["post"].="--{$bounbary}\r\n"; | |
$return["post"].="Content-Disposition: form-data; name=\"{$key}\"; filename=\"{$baseName}\"\r\n"; | |
$return["post"].="Content-Type: application/octet-stream\r\n"; | |
$return["post"].="\r\n"; | |
$return["post"].=file_get_contents($filePath)."\r\n"; | |
}else{ | |
$return["post"].="--{$bounbary}\r\n"; | |
$return["post"].="Content-Disposition: form-data; name=\"{$key}\"\r\n"; | |
$return["post"].="\r\n"; | |
$return["post"].="{$value}\r\n"; | |
} | |
} | |
$return["post"].="--{$bounbary}--\r\n"; | |
return $return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment