使用curl上传图片

时间:2017-05-29 05:23:16

标签: php google-maps curl

$ curl --request POST \
        --url 'UPLOAD_URL' \
        --upload-file 'PATH_TO_FILE' \
        --header 'Authorization: Bearer YOUR_ACCESS_TOKEN'

如何在php中转换此卷曲代码

我正在使用这种方式,但它给了我错误......

$file_name =
 "http://stview.agencia.co.jp:8080/STVIEW/main/google-api-php/test/image/1.jpg";
 // $file_name = 
 "/var/www/stview/public_html/STVIEW/main/google-api-php/test/image/1.jpg";

 if(function_exists('curl_file_create')) {
   $cFile = curl_file_create($file_name); 

 } else {
   $cFile = '@' . realpath($file_name); 

 }

 $post = array('extra_info' => '123456','file_contents'=> $cFile);
 $data = array('name' => 'Foo', 'file' =>
 '@/var/www/stview/public_html/STVIEW/main/google-api-php/test/image/1.jpg');


 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,
 'https://streetviewpublish.googleapis.com/media/user/117051626264904472027/photo/2668839193146023612');

 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
       "authorization: Bearer ya29.GltZBMH9XSn56qs5PHsYpjC-WfFeff9dpnDXr1I-xtN0L8wNRQWVRZsgzEShAFij4N7u7uDXPW4UA5yZ9qqy3dgWjxawRHlhQ0OgwDOJetyj9cSwEah5HZ6NnzJQ",
       "content-type: application/json"   ));

 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 // curl_setopt($ch, CURLOPT_UPLOAD, true);
 // curl_setopt($ch, CURLOPT_POST, true); // enable posting
 // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLINFO_HEADER_OUT, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);


 $info = curl_getinfo($ch);
 $result=curl_exec ($ch);
 $err = curl_error($ch);

 echo "info-" . print_r($info) . "<br>\n"; echo "result-" . $result .
 "<br>\n"; echo "error-" . $err . "<br>\n"; var_dump($result) ;
 print_r($result);

请告诉我我在哪里做错了。

我的输出是 请求中找不到文件。

0 个答案:

没有答案