2016-09-21 151 views
0

我试图在PHP中发送POST请求,但帖子字段在URL中。上述如何使用php在HTTP POST请求中发送参数?

$project_no  = 1711; 
$group_name  = "name of group"; 

$url = urlencode("http://api.example.org/v1/projects?auth_token=xsxxex-xxmczx-66mvmc-9133&project_no=" . $project_no . "&group_name=" . $group_name); 

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
$response = curl_exec($ch); 
curl_close($ch); 

var_dump($response) 

代码返回,说

bool(false) 

一个错误我在做什么错在这里?

回答

0

您需要包括增值经销商为postfields并将它们作为一个额外的卷曲参数:第一

$data = array('project_no' => $project_no, 'group_name' => $group_name); 

curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 

如果应用程序需要JSON,您可以json_encode()的$数据。

$json = json_encode($data); 

curl_setopt($ch, CURLOPT_POSTFIELDS, $json); 

我不知道你的应用程序的具体细节,但很可能,该AUTH_TOKEN可能需要太发送作为授权头:

$headers = array(
       'Authorization': 'xsxxex-xxmczx-66mvmc-9133', 
       //etc); 


    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
+0

我得到了一个错误说“{”消息“: “找不到与请求URI'http://api.example.org/p/v1/projects'匹配的HTTP资源。”}“ – Tsea