title: Send HTTP GET Request in PHP (Don't Return Target URL's Contents, Just Execute It)
body: <p>I have been struggling against a really very weird scenario in PHP. I have a main script which creates some HTML (including a google graph). But it has some <code>&lt;SCRIPT&gt;</code> tags that won't be rendered by MPDF library. So here's what I did:</p>
  
  <p>Created a new file <code>g_chart.php</code> and included all the graph generating code in it as follows:</p>
  
  <pre><code>&lt;meta charset="UTF-8"&gt;
  
  &lt;script&gt;
      var save_as_file = '&lt;?php echo $_GET['file']; ?&gt;';
  &lt;/script&gt;
  
  &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="https://www.google.com/jsapi"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="http://canvg.googlecode.com/svn/trunk/rgbcolor.js"&gt;&lt;/script&gt; 
  &lt;script type="text/javascript" src="http://canvg.googlecode.com/svn/trunk/canvg.js"&gt;&lt;/script&gt;
  
  &lt;script&gt;
  
  google.load('visualization', '1', {packages: ['corechart', 'bar']});
  google.setOnLoadCallback(drawBasic);
  
  function drawBasic() {
  
        var data = google.visualization.arrayToDataTable([
          ['City', '2010 Population',],
          ['حواس', 8175000],
          ['انسانی جسم کے حصوں کے نام اور افعال', 3792000],
          ['Chicago, IL', 2695000],
          ['Houston, TX', 2099000],
          ['صوتیات : ہجے اور الفاظ', 1526000]
        ]);
  
        var options = {
          title: 'Population of Largest U.S. Cities',
          chartArea: {width: '50%'},
          hAxis: {
            title: 'Total Population',
            minValue: 0
          },
          vAxis: {
            title: 'City'
          }
        };
  
        var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
  
        google.visualization.events.addListener(chart, 'ready', function () {
            chart_div.innerHTML = '&lt;img src="' + chart.getImageURI() + '"&gt;';
        });
  
        chart.draw(data, options);
  
        var img_src = $('#chart_div img').attr('src');
        $('#chart_div').after('&lt;form name="frm_img"&gt;&lt;input type="hidden" name="img_src" value="'+img_src+'"&gt;&lt;/form&gt;'); 
  
        SaveToDisk();
  }
  
  
  function SaveToDisk() {
  
      var oform = $('form[name="frm_img"]');
      var datastring = oform.serialize();
  
      $.ajax({
          type    : "POST",
          url     : "g_chart_save.php?file="+save_as_file,
          data    : datastring,
          cache   : false,
          async   : true,
          success : function(html){
  
          }
      });
  
  }
  
  &lt;/script&gt;
  
  &lt;div id="chart_div"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>g_chart_save.php file is as follows:</p>
  
  <pre><code>&lt;?php
  
  if (!empty($_POST)) {
  
      $img_src = $_POST['img_src'];
      $file = $_GET['file'];
  
      $image = base64_to_jpeg( $img_src, $file.'.png' );
  
  }
  
  function base64_to_jpeg($base64_string, $output_file) {
      $ifp = fopen($output_file, "wb"); 
  
      $data = explode(',', $base64_string);
  
      fwrite($ifp, base64_decode($data[1])); 
      fclose($ifp); 
  
      return $output_file; 
  }
  
  ?&gt;
  </code></pre>
  
  <p>So basically, when I call <code>g_chart.php?file=graph</code> in the browser. My graph is generated and saved to same directory as a physical <code>graph.png</code> file.</p>
  
  <p>By creating above work-around, I now have to call this graph generating script <code>g_chart.php?file=graph</code> from my main script's PHP file (while it is being executed i.e. runtime).</p>
  
  <p>I have used following methods to make it happen:</p>
  
  <ol>
  <li>CURL</li>
  <li>fopen</li>
  <li>file_get_contents</li>
  <li>stream_context_create</li>
  <li>HttpRequest</li>
  </ol>
  
  <p>Everytime I use any of the above methods, it hits the target file <code>g_chart.php</code> but it actually doesn't execute it rather it returns me the contents or code that I have written in target file.</p>
  
  <p>I want it to be executed SILENTLY, so that my graph's image file is created and I can then include it in my PDF.</p>
  
  <p>Remember, I will be passing my graph data to target file in a GET request later, so as to make it dynamic.</p>
  
  <p>Any help would be much appreciated.</p>