Note that there are some explanatory texts on larger screens.

plurals
  1. POphp file_get_contents or curl not working
    primarykey
    data
    text
    <p>I want to get data from bseindia.com by using curl but I am getting only null as data..</p> <p>here's my curl function code:</p> <pre><code> &lt;?php function load($url, $ispost=0, $data='', $header=array()){ if($url=='' || ($ispost &amp;&amp; $data=='')) return; $host=parse_url($url, PHP_URL_HOST); $header[]='User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:6.0) Gecko/20100101 Firefox/6.0'; $header[]='Host: '.$host; $header[]='Connection: keep-alive'; $header[]='Referer: http://'.$host.'/'; if($ispost) $header[]='Content-length: '.strlen($data); $ch = curl_init("$url"); if($ispost){ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_VERBOSE, 0); curl_setopt($ch, CURLOPT_FAILONERROR, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); $file = curl_exec($ch); $header = curl_getinfo($ch); curl_close($ch); return array('http_code'=&gt;$header['http_code'], 'output'=&gt;$file); } ?&gt; </code></pre> <p>and here's my calling:</p> <pre><code>&lt;?php $data = load('http://www.bseindia.com/', 0, ''); if($data['http_code']!==200) exit(json_encode(array('err'=&gt;true, 'msg'=&gt;'Unable to get! Error='.$data['http_code']))); exit(json_encode(array('err'=&gt;false, 'msg'=&gt;json_decode($data['output'])))); ?&gt; </code></pre> <p>but my output in json is:</p> <pre><code>{"err":false,"msg":null} </code></pre> <p>but if I change</p> <pre><code>curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); </code></pre> <p>to</p> <pre><code>curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); </code></pre> <p>then I am getting the output directly printed but not stored in variable, why??</p> <p>So, my question is how do I get data without using:</p> <pre><code>curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); </code></pre> <p>???</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload