我有以下php代码
curl_setopt($ch, CURLOPT_URL, $URL); curl_setopt($ch, CURLOPT_USERAGENT, $this->_agent); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, true); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $this->_headers); curl_setopt($ch, CURLOPT_ENCODING , "gzip"); curl_setopt($ch, CURLOPT_VERBOSE, false); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 120); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_COOKIEFILE, $this->_cookie_file_path); curl_setopt($ch, CURLOPT_COOKIEJAR, $this->_cookie_file_path); curl_setopt($ch, CURLOPT_POSTFIELDS, '{"folderId":"1","parameters":{"amount":3,"ascending":false,"offset":0,"sort":"date"}}'); curl_setopt($ch, CURLOPT_POST, 1);
但我不明白为什么不工作。我发布JSON的API表示没有收到参数。我的代码有什么问题吗?我认为整个技巧都是关于JSON参数...我不知道如何发送它们,因为我看不到与http分析器的任何“nave->值”对,因为它通常以简单的形式出现。只是没有任何“名字”的JSON代码。
答案 0 :(得分:3)
你可以使用它并替换为
DelieveryTableViewController
使用此
prepareForSegue
答案 1 :(得分:3)
您可以尝试以下方法。
基本上,如果我们有一个数据数组,那么它应该是$
使用php json encoded
。您还应该在json_encode
中添加content-type
,可以在curl中定义为header
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
答案 2 :(得分:2)
如果:你使用post方法,你应该知道:
CURLOPT_POST TRUE执行常规HTTP POST。此POST是普通的application / x-www-form-urlencoded类型,最常用于HTML表单。
@see http://php.net/manual/en/function.curl-setopt.php
所以:你可以这样做:
$ar_form = array('name'=>'PHPJungle','age'=>66,'gender'=>'male');
$poststr = http_build_query($ar_form ); # important
$options[CURLOPT_HTTPGET] = false;
$options[CURLOPT_POST] = true;
$options[CURLOPT_POSTFIELDS] = $poststr ; //default type:application/x-www-from-urlencoded
curl_setopt_array ( $ch, $options );
# @todo your other codes
这是我已经使用了很长时间的课程。该课程基于PHP cURL。 它支持GET / POST,HTTP / HTTPS。
答案 3 :(得分:2)
您可以使用curl来发布json数据,如下所示:
使用命令提示符:
curl -X POST -H "Content-Type: application/json" -d '{"folderId":"1","parameters":{"amount":3,"ascending":false,"offset":0,"sort":"date"}}' $url
使用PHP:
$data = array("folderId"=>"1","parameters"=>array("amount"=>3,"ascending"=>false,"offset"=>0,"sort"=>"date"));
$postdata = json_encode($data);
OR
$postdata = '{"folderId":"1","parameters":{"amount":3,"ascending":false,"offset":0,"sort":"date"}}';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
答案 4 :(得分:1)
您尚未设置内容类型,因此发布数据将作为表单数据发送。尝试将内容类型设置为application / json。
如果这不起作用,请尝试用数组包装json字符串。
答案 5 :(得分:1)
jmeter -Jjmeter.save.saveservice.thread_name=true -n -t example.jmx -l example.jtl
这适用于json decode将json字符串转换为数组。
$link = "http://test.domain/myquery";
$json = '{"folderId":"1","parameters":{"amount":3,"ascending":false,"offset":0,"sort":"date"}}';
$postdata = json_decode($json);
echo openurl($link, $postdata);
答案 6 :(得分:1)
如果使用正文的API端点使用json数据发送请求,则可以使用Guzzle文档在此处doc。
use GuzzleHttp\Client;
$client = new Client();
$request = $this->client->post($url,array(
'content-type' => 'application/json'
),array());
$request->setBody($json_data);
$response = $request->send();
return $response;
希望这项工作。
答案 7 :(得分:0)
我不确定,这是解决方案,但是当发布json时,这适用于我,从
更改json'{"folderId":"1","parameters":{"amount":3,"ascending":false,"offset":0,"sort":"date"}}'
到
"{'folderId':'1','parameters':{'amount':3,'ascending':false,'offset':0,'sort':'date'}}"
我做的唯一改变是双引号现在在外面,这对我有用,但我显然是张贴到另一台服务器
我可以提供的其他帮助是下载网络调试工具,例如Fiddler
或Charles
代理,并监控发送/接收的请求,可能是您的其他错误的情况码。
希望我帮助过:)
答案 8 :(得分:0)
你可以按步骤做到:
$data = array(
'folderId'=>"1","parameters"=>array(
"amount"=>"3","ascending"=>false,"offset"=>0,"sort"=>"date"
)
);
$data_string = http_build_query($data);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($data));
curl_setopt($ch,CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result,true);
不知道是否需要标题,我认为默认已经是application / x-www-form-urlencode
id它不起作用,尝试更改数组中的$ data值。认为有帮助。 :)
答案 9 :(得分:0)
首先 请检查curl http状态代码
$http_code= curl_get_info($exec_res,CURL_HTTP_CODE);
然后使用post header API服务器修改此请求标头集添加记录器以记录这些请求信息。