找到你要的答案

Q:URL encoding JSON strings when calling Facebook MarketingAPI

Q:URL编码JSON字符串,当调用脸谱网marketingapi

When calling an API endpoint in PHP, what is the difference between passing as a parameter in the URL using

$str = '{\'since\':\'2015-01-01\',\'until\':\'2015-01-22\'}';
'blahblah?access_token=xxx&time_range=' . $str;

versus

 $str = '{\'since\':\'2015-01-01\',\'until\':\'2015-01-22\'}';
'blahblah?access_token=xxx&time_range=' . urlencode($str);

Apart from the latter making more likely the URL becoming 'too long'? Knowing that no one is going to see the URL, I just want to retrieve data from the API and output to CSV

Many thanks

当调用一个API端点在PHP中,作为参数传递的URL使用之间的区别是什么

$str = '{\'since\':\'2015-01-01\',\'until\':\'2015-01-22\'}';
'blahblah?access_token=xxx&time_range=' . $str;

 $str = '{\'since\':\'2015-01-01\',\'until\':\'2015-01-22\'}';
'blahblah?access_token=xxx&time_range=' . urlencode($str);

除了后者更容易使URL变得“太长”?我知道没有人会看到网址,我只是想从API输出检索数据为CSV文件

谢谢

answer1: 回答1:

via something like this:

$data = array("name" => "Hagrid", "age" => "36");                                                                    
$data_string = json_encode($data);                                                                                   

$ch = curl_init('http://api.local/rest/users');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
'Content-Type: application/json',                                                                                
'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

$result = curl_exec($ch);

instead of passing this information in the url via a GET request?

Edit: Actually, can someone just tell me where I can read up on curl command line flags like -G, or how to implement the below in PHP? Thanks

curl -G \
-d "fields=['impressions','campaign_group_name','cost_per_action_type']" \
-d "action_breakdowns=['action_type','action_carousel_card_name']" \
-d "level=adgroup" \
-d "access_token=<ACCESS_TOKEN>" \
"https://graph.facebook.com/<API_VERSION>/act_<AD_ACCOUNT_ID>/insights"

通过这样的东西:

$data = array("name" => "Hagrid", "age" => "36");                                                                    
$data_string = json_encode($data);                                                                                   

$ch = curl_init('http://api.local/rest/users');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
'Content-Type: application/json',                                                                                
'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

$result = curl_exec($ch);

而不是通过一个GET请求在URL中传递这些信息?

编辑:其实,谁能告诉我哪能读上卷曲的命令行标志- G,或如何实现在PHP?谢谢

curl -G \
-d "fields=['impressions','campaign_group_name','cost_per_action_type']" \
-d "action_breakdowns=['action_type','action_carousel_card_name']" \
-d "level=adgroup" \
-d "access_token=<ACCESS_TOKEN>" \
"https://graph.facebook.com/<API_VERSION>/act_<AD_ACCOUNT_ID>/insights"
php  json  facebook  url-encoding