douyun1852
2016-12-15 10:53
浏览 60
已采纳

通过cURL从外部网站获取一些数据

I want to pull some html pice of code with some data from an external website with cURL, but a recived a null response. If I type the url in browser i get the data that i want but i need to do that from my app via cURL or file_get_contents();

This is my code :

    $_awb = '888000074599';
    $url = 'http://89.45.196.45';
    $post_fields = ':8080/?id=8IM*8J*9K&NT='.$_awb;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);

    $result = curl_exec($ch);
    curl_close($ch);
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drpmazn9021 2016-12-15 11:05
    已采纳

    You should not be using CURLOPT_POSTFIELDS. You are trying to do a GET request with a query string, but postfields is intended for making POST requests.

    So setting CURLOPT_URL to the proper (absolute) URL should work.

        $url = "http://89.45.196.45:8080/?id=8IM*8J*9K&NT="
        $nt = "888000074599";
    
        $ch = curl_init();
    
        curl_setopt($ch, CURLOPT_URL, $url . $nt);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
        $result = curl_exec($ch);
        curl_close ($ch);
    
    打赏 评论

相关推荐 更多相似问题