2016-02-03 20:16
浏览 277


I'm attempting to execute a curl statement in PHP that uses a JSON array. I'll post my code below with a little explanation of what im trying to do

function doPost($url, $user, $password, $params) {
  $authentication = 'Authorization: Basic '.base64_encode("$user:$password");
  $http = curl_init($url);
  curl_setopt($http, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($http, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($http, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($http, CURLOPT_URL, $url);
  curl_setopt($http, CURLOPT_POST, true);
  curl_setopt($http, CURLOPT_POSTFIELDS, $params);
  curl_setopt($http, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json', $authentication));
  return curl_exec($http);

$link = "http://link.it/i.htm?id=55&key=23987gf2389fg";
$phone = '5551231234';
$phone = '1' . $phone;

//Write message
$msg = "Click here " . $link;
$params = '[{"phoneNumber":"'.$phone.'","message":"'.$msg.'"}]';

//Send message
$return = doPost('https://api.link.com','username','password',$params);
echo $return;

Params ends up being

$params = '[{"phoneNumber":"15551231234","message":"Click here http://link.it/i.htm?id=55&key=23987gf2389fg"}]';

All looks good. And the JSON array that is being created by params actually works perfectly if there is no link inside the $msg variable. I am able to execute a successful CURL call. The only time it fails is when I add a link to my $msg variable.

I have contact the support team for the API and they inform me that everything should work on their end.

At this point I am guessing that the link needs to be escaped somehow before it can be written into the JSON array. I have tried escaping the colons and forward slashes with back slashes, but it does not fix the problem. Is there anyone that can shed some light on how to pass a url?

Thank you in advance!!

图片转代码服务由CSDN问答提供 功能建议

我正在尝试在PHP中执行使用JSON数组的curl语句。 我将在下面发布我的代码,稍微解释我想要做什么

  function doPost($ url,$ user,$ password,$ params){
 $  authentication ='Authorization:Basic'.base64_encode(“$ user:$ password”); 
 $ http = curl_init($ url); 
 curl_setopt($ http,CURLOPT_SSL_VERIFYHOST,false); 
 curl_setopt($ http,CURLOPT_SSL_VERIFYPEER  ,false); 
 curl_setopt($ http,CURLOPT_RETURNTRANSFER,true); 
 curl_setopt($ http,CURLOPT_URL,$ url); 
 curl_setopt($ http,CURLOPT_POST,true); 
 curl_setopt($ http,CURLOPT_POSTFIELDS,  $ params); 
 curl_setopt($ http,CURLOPT_HTTPHEADER,数组(
'Content-Type:application / json',$ authentication)); 
返回curl_exec($ http); 
 $ link  =“http://link.it/i.htm?id=55&key=23987gf2389fg";
$phone ='5551231234'; 
 $ phone ='1'。  $ phone; 
 $ msg =“点击此处”。  $ link; 
 $ params ='[{“phoneNumber”:“'。$ phone。'”,“message”:“'。$ msg。'”}]'; 
  $ return = doPost('https://api.link.com','username','password',$params); 
echo $ return; 


  $ params ='[{“phoneNumber”:“15551231234”,“message”:“点击这里http://link.it/i。  htm?id = 55& key = 23987gf2389fg“}]'; 

一切看起来都不错。 如果$ msg变量中没有链接,那么由params创建的JSON数组实际上可以正常工作。 我能够执行成功的CURL调用。 它失败的唯一时间是我添加了一个指向我的$ msg变量的链接。

我已经联系API的支持团队,他们告诉我一切都应该在他们的最后工作。< / p>

此时我猜测链接需要以某种方式进行转义才能写入JSON数组。 我已经尝试用反斜杠转义冒号和正斜杠,但它不能解决问题。 有没有人能说明如何传递网址?


  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dsv38843 2016-02-03 20:22

    Don't construct your JSON manually. Construct and array or object then call json_encode() on it.

    $params = array();
    $object = array("phone"=>$phone, "message"=>$linkMsg);
    $params[] = (object) $object;
    $param_json_string = json_encode($params);

    Then when submitting the JSON via POST with curl, you need to specify the lenght of the string in the header.

    curl_setopt($http, CURLOPT_HTTPHEADER, 
                array( 'Content-Type: application/json', 
                       'Content-Length: '. strlen($param_json_string)));           

    Of course, this is in addition to other headers like authentication you're setting (as I see you do in your doPost() method).

    解决 无用
    打赏 举报

相关推荐 更多相似问题