duanpu6319 2016-02-03 20:16
浏览 305
已采纳

将URL传递给JSON数组

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!!

  • 写回答

1条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 ubuntu18.04 tty报错&tty界面su/sudo命令无法执行,如何解决?
      • ¥20 关于c语言网络编程,实现传文件和即时聊天
      • ¥20 下面的压缩方法是否可行
      • ¥15 结构体数组读取文件信息失败,读取不了
      • ¥15 kaldi thchs30 训练遇到问题
      • ¥15 shellter无法使用,如何解决?(操作系统-kali)
      • ¥15 matlab动态模态分解程序
      • ¥60 关于渗透及远控的几个问题
      • ¥15 python文本词汇出现次数统计
      • ¥15 使用按键和串口,记录按键在不消抖的情况下触发的次数