cURL不发送POST数据,但在终端正常工作 [英] cURL not sending POST data, but works properly in terminal

查看:285
本文介绍了cURL不发送POST数据,但在终端正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在命令行中工作的curl代码:

this is the curl code that works in command line :

  $ curl -F file=@/path/to/index.html -u lslkdfmkls@gmail.com -F 'data={"title":"API V1  App","package":"com.alunny.apiv1","version":"0.1.0","create_method":"file"}' https://build.phonegap.com/api/v1/apps

这是我的代码:

$ch = curl_init();

$data = array("title"=>"sampele title","package"=>"com.fsdlfn.sdfknsdj","version"=>"0.1.0","create_method"=>"file","file"=>"@/path/myfolder/myfile.zip");

$jsdata = json_encode($data);

curl_setopt($ch, CURLOPT_URL, 'https://build.phonegap.com/api/v1/app?auth_token='.$token); //got this token already, so using that here .
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsdata);

echo curl_exec($ch);

curl命令在终端工作,但上面的curl命令写成php,返回错误:

The curl command working in terminal, but the above curl command written in php, returning error :

{"error":"no create_method specified: file, remote_repo, or hosted_repo"}

解决方案是什么?

感谢

推荐答案


$ b

Try this:

$ch = curl_init();

$data = array("title"=>"sampele title","package"=>"com.fsdlfn.sdfknsdj","version"=>"0.1.0","create_method"=>"file");

$jsdata = json_encode($data);

curl_setopt($ch, CURLOPT_URL, 'https://build.phonegap.com/api/v1/app?auth_token='.$token); //got this token already, so using that here .
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => $jsdata, 'file'=>'@/path/myfolder/myfile.zip'));

echo curl_exec($ch);

以下是我修正错误的原因:

Here's why I made the fixes:


  1. $ data 正在 json_encode d,因此cURL不会看到 file 属性,它将作为字符串发送,而不是读取文件 - 这就是为什么我把文件 POSTFIELDS

  2. 变量 $ data 不会导致 data = ... ,而是单个字符串(如 POST XML)。

  1. $data is being json_encoded, so cURL will not see the file attribute and it'll be sent as a string, rather then reading the file - this is why I put file directly in the POSTFIELDS
  2. The variable $data will not cause it to be sent as data=... but rather a single string (like POSTing XML).

这篇关于cURL不发送POST数据,但在终端正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆