2017-03-18 20:28
浏览 215

PHP CURL在循环中获得400错误

here the foreach loops returns a phone number which goes in the link but whenever I try to run this script it gives me the error 400 bad request. I have also tried using the useragent but even that does not solve the error. any help would be great.

PS. this is my first question please let me know if I can provide anything to solve this problem.

foreach($var5 as $number) {
     $ch = curl_init();
     echo $ch1;
     curl_setopt($ch, CURLOPT_URL, "$ch1");
     curl_setopt($ch, CURLOPT_HEADER, 0);
     //curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36');


When I add urlencode the bad request definitely goes but I want this url to execute: 9408644603&msg=hey but instead this url is executed:

Also here I have only applied urlencode to the phone number rest of the fields I'm entering myself directly. Also the bad request probably goes as the api won't execute if the phone number is not correct.

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

这里foreach循环返回一个链接的电话号码但是每当我尝试运行这个脚本时它给了我 错误400错误请求。 我也尝试过使用useragent,但即使这样也无法解决错误。 任何帮助都会很棒。

PS。 这是我的第一个问题请告诉我是否可以提供解决此问题的任何内容。

  foreach($ var5 as $ number){
 $ ch = curl_init()  ; 
 $ ch1 =“$number&msg=hey”; 
 echo $ ch1; \  n curl_setopt($ ch,CURLOPT_URL,“$ ch1”); 
 curl_setopt($ ch,CURLOPT_HEADER,0); 
 // curl_setopt($ ch,CURLOPT_USERAGENT,'Mozilla / 5.0(Windows NT 10.0; WOW64)AppleWebKit /  537.36(KHTML,与Gecko一样)Chrome / 56.0.2924.87 Safari / 537.36'); 
 curl_exec($ ch); 
 curl_close($ ch); 
 \  n 


当我添加 urlencode 时,错误的请求肯定会发生,但我想要这个url执行: 9408644603& msg = hey 但是这个网址被执行: http ://移动= 1234567890&安培;传=密码&安培 ; senderid = ID& to =%2B + 9408644603& msg = hey

此处我只将 urlencode 应用于电话号码其余部分 我直接进入自己的领域。 如果电话号码不正确,那么糟糕的请求也可能是因为api不会执行。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpql57753 2017-03-19 20:15

    The + in the phone numbers for some reason is breaking the receiving applications system. URL encoding doesn't resolve this because they only want numbers. You can use a regex to replace all the leading, non-numerical, characters with nothing.

    preg_replace('/^\D+/','', $serverthing)

    \D is a non number (\d is a number).
    ^ is the start of the string.
    + is one or more of the previous character/group.
    /s are delimiters showing the start and end of the regex.

    点赞 打赏 评论

相关推荐 更多相似问题