file_get_contents - 找不到文件



我在这篇文章中跟进了解决方案:

https://stackoverflow.com/a/4247082/1235655

我收到以下错误:

{"success":false,"code":3003,"version":1,"message":"File not found","data":null}

我错过了什么?

这是代码:

$boundary = 'DE(io5Kd5)NB@DQ22CeCfC4p';
define('MULTIPART_BOUNDARY', $boundary);
$header = array('Authorization' => 'Bearer ' . get_id_token(),'Content-Type' => 'Content-Type: multipart/form-data; boundary='.MULTIPART_BOUNDARY);
define('FORM_FIELD', 'file');
$filename = "generated(4).json";
$file_contents = file_get_contents($filename);
$content =  "--".MULTIPART_BOUNDARY."rn".
"Content-Disposition: form-data; name="".FORM_FIELD.""; filename="".basename($filename).""rnn".
$file_contents."rn";
$content .= "--".MULTIPART_BOUNDARY."--rn";
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => $header,
'content' => $content,
)
));
echo file_get_contents('https://mpop-sit.example.com/product/api/products/import', false, $context);

这是$content的样子:

--DE(io5Kd5)NB@DQ22CeCfC4p
Content-Disposition: form-data; name="file"; filename="generated(4).json"
[
{
"categoryId": 80077027,
"merchant": "b0bc3022-f76a-48ca-9a60-07b33efb90e9",
"attributes": {
"merchantSku": "CODE-SKU-INT-1",
"VaryantGroupID": "Hepsiburada2",
"Barcode": "8681080599169s",
"UrunAdi": "Roth Tyler",
"UrunAciklamasi": "Duis enim duis magna ex veniam elit id Lorem cillum minim nisi id aliquip. Laboris magna id est et deserunt adipisicing tempor eu ea officia ipsum deserunt. Irure occaecat sit aliquip elit ipsum sint dolore quis est amet aute pariatur cupidatat fugiat. Cillum pariatur pariatur occaecat sint. Aliqua qui in exercitation nulla aliquip id ipsum aliquip ad ut excepteur culpa consequat aliquip. Nisi ut ex tempor enim adipisicing anim irure pariatur.rn",
"Marka": "Tisort.ist",
"tax_vat_rate": "8",
"GarantiSuresi": 24,
"kg": "1",
"Image1": "https://productimages.example.net/s/27/552/10194862145586.jpg",
"Image2": "https://productimages.example.net/s/27/552/10194862145586.jpg",
"Image3": "https://productimages.example.net/s/27/552/10194862145586.jpg",
"Image4": "https://productimages.example.net/s/27/552/10194862145586.jpg",
"Image5": "https://productimages.example.net/s/27/552/10194862145586.jpg",
"kesim_kalibi": "Regular Fit",
"kullanici_cinsiyeti": "Erkek",
"malzeme_": "Pamuklu",
"yaka_stili": "Bisiklet Yaka",
"renk_variant_property": "Kırmızı",
"beden_variant_property": "XS",
"adet_variant_property": 99999
}
},
{
"categoryId": 80077027,
"merchant": "b0bc3022-f76a-48ca-9a60-07b33efb90e9",
"attributes": {
"merchantSku": "CODE-SKU-INT-2",
"VaryantGroupID": "Hepsiburada2",
"Barcode": "8681080599121s",
"UrunAdi": "Roth Tyler",
"UrunAciklamasi": "Duis enim duis magna ex veniam elit id Lorem cillum minim nisi id aliquip. Laboris magna id est et deserunt adipisicing tempor eu ea officia ipsum deserunt. Irure occaecat sit aliquip elit ipsum sint dolore quis est amet aute pariatur cupidatat fugiat. Cillum pariatur pariatur occaecat sint. Aliqua qui in exercitation nulla aliquip id ipsum aliquip ad ut excepteur culpa consequat aliquip. Nisi ut ex tempor enim adipisicing anim irure pariatur.rn",
"Marka": "Tisort.ist",
"tax_vat_rate": "8",
"GarantiSuresi": 24,
"kg": "1",
"Image1": "https://productimages.example.net/s/27/552/10194862145586.jpg",
"Image2": "https://productimages.example.net/s/27/552/10194862145586.jpg",
"Image3": "https://productimages.example.net/s/27/552/10194862145586.jpg",
"Image4": "https://productimages.example.net/s/27/552/10194862145586.jpg",
"Image5": "https://productimages.example.net/s/27/552/10194862145586.jpg",
"kesim_kalibi": "Regular Fit",
"kullanici_cinsiyeti": "Erkek",
"malzeme_": "Pamuklu",
"yaka_stili": "Bisiklet Yaka",
"renk_variant_property": "Mavi",
"beden_variant_property": "XL",
"adet_variant_property": 99999
}
},
{
"categoryId": 80077027,
"merchant": "b0bc3022-f76a-48ca-9a60-07b33efb90e9",
"attributes": {
"merchantSku": "CODE-SKU-INT-3",
"VaryantGroupID": "Hepsiburada3",
"Barcode": "8690605661571s",
"UrunAdi": "Roth Tyler",
"UrunAciklamasi": "Duis enim duis magna ex veniam elit id Lorem cillum minim nisi id aliquip. Laboris magna id est et deserunt adipisicing tempor eu ea officia ipsum deserunt. Irure occaecat sit aliquip elit ipsum sint dolore quis est amet aute pariatur cupidatat fugiat. Cillum pariatur pariatur occaecat sint. Aliqua qui in exercitation nulla aliquip id ipsum aliquip ad ut excepteur culpa consequat aliquip. Nisi ut ex tempor enim adipisicing anim irure pariatur.rn",
"Marka": "Tisort.ist",
"tax_vat_rate": "8",
"GarantiSuresi": 24,
"kg": "1",
"Image1": "https://productimages.example.net/s/27/552/10194862145586.jpg",
"Image2": "https://productimages.example.net/s/27/552/10194862145586.jpg",
"Image3": "https://productimages.example.net/s/27/552/10194862145586.jpg",
"Image4": "https://productimages.example.net/s/27/552/10194862145586.jpg",
"Image5": "https://productimages.example.net/s/27/552/10194862145586.jpg",
"kesim_kalibi": "Regular Fit",
"kullanici_cinsiyeti": "Erkek",
"malzeme_": "Pamuklu",
"yaka_stili": "Bisiklet Yaka",
"renk_variant_property": "Sarı",
"beden_variant_property": "XL",
"adet_variant_property": 99999
}
}
]
--DE(io5Kd5)NB@DQ22CeCfC4p--

这是 CURL 版本,它给出了相同的错误:

$request = curl_init('https://mpop-sit.example.com/product/api/products/import');
curl_setopt($request, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.get_id_token()
));
// send a file
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
$request,
CURLOPT_POSTFIELDS,
array(
'file' => '@' . realpath('generated(4).json')
));
// output the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);
// close the session
curl_close($request);

也许这篇文章中的例子会对你有所帮助 https://www.php.net/manual/en/class.curlfile.php#115569

添加用户代理会有所帮助,也许 api 正在检查该值以确保请求有效。将CURLOPT_SSL_VERIFYPEER设置为 false,当您尝试向 https 链接发出请求时会有所帮助

使用 curl_file_create(( 添加文件

很难回答您的问题,因为我对您正在使用的 API 一无所知。

相关内容

  • 没有找到相关文章

最新更新