【问题标题】:want to execute a url without loading it in browser in php想要执行一个url而不用php在浏览器中加载它
【发布时间】:2014-08-20 13:31:54
【问题描述】:

现在我在为短信网关执行 api 时遇到问题。他们让我点击一个网址,它是http://bulksmsgateway.in/sendmessage.php?user=........&password=.......&mobile=........&message=.......&sender=.......&type=3

但我必须这样做而不在浏览器中加载此 url。所以我使用了 curl 方法。我在哪里收到 400 错误请求错误。这是一个支持 PHP 的 Windows 服务器。

我的代码在下面 -

$ch = curl_init();

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (兼容; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_URL, $url);

$contents = curl_exec($ch);

$ee = curl_getinfo($ch);

print_r($ee);

curl_close($ch);

if ($contents){ echo 'sent'; print_r($内容); }else{ 回声“丢失”; }

$ee 返回

数组 ( [url] => http://bulksmsgateway.in/sendmessage.php?user=usrname&password=pwd&mobile=9126050xxx&message=Thanking您通过 FCI 订购。您的订单将按时交付。您的订单号是 000198。请保留以备将来参考。&sender=SUBANK&type=3

[content_type] => text/html; charset=iso-8859-1
[http_code] => 400
[header_size] => 145
[request_size] => 411
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.094
[namelookup_time] => 0
[connect_time] => 0.047
[pretransfer_time] => 0.047
[size_upload] => 0
[size_download] => 299
[speed_download] => 3180
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => 0
[starttransfer_time] => 0.094
[redirect_time] => 0
[certinfo] => Array
    (
    )

[redirect_url] => 

)

我认为问题出在消息部分。 url中消息中的空格出现错误。因为没有空格的消息发送成功。 &nbsp 也无法正常工作。请使用句号(。)。请在这方面帮助我。

提前致谢。

【问题讨论】:

  • 您的用户名和密码不包含 URL 中保留的特殊字符。所以请转换 &,?到他们的代码,例如 %26 for &

标签: php curl


【解决方案1】:

我已经用 %20 替换了空格,这对我来说很好用。谢谢大家的帮助...

【讨论】:

    【解决方案2】:

    您应该以这种方式对 message 查询参数进行编码:

    $message = urlencode("Thanking you for ordering through FCI.Your Order will be delivered on time.Your order no is 000198.Please keep it for future");
    

    这是一个 message 参数,其中包含正确的编码作为 GET 参数发送:

    Thanking+you+for+ordering+through+FCI.Your+Order+will+be+delivered+on+time.Your+order+no+is+000198.Please+keep+it+for+future

    【讨论】:

      【解决方案3】:

      http://www.technologyworkshops.net/tutorials-f39/curl-using-get-method-with-php-t135.html

      function curlWithGetMethod($url, $data)
          {
          $fields_string = '';
      
          foreach($data as $key=>$value){
          $fields_string[]=$key.'='.urlencode($value).'&'; }
          $urlStringData = $url.'?'.implode('&',$fields_string);
      
          $ch = curl_init();
      
          curl_setopt($ch, CURLOPT_HEADER, 0);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
          curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,10); 
         curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
          curl_setopt($ch, CURLOPT_URL, $urlStringData ); 
      
          $return = curl_exec($ch);
          curl_close($ch);
      
          return $return;
      }
      
      $data = array('first_name' => 'myname', 'email' => 'example@example.com', 'phone' => '0123456789',  );
      echo curlWithGetMethod(''http://www.technologyworkshops.net/', $data);
      

      【讨论】:

        猜你喜欢
        • 2019-04-22
        • 2013-07-11
        • 2021-11-07
        • 1970-01-01
        • 1970-01-01
        • 2012-01-03
        • 2020-08-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多