I
I
Igor Shumilovsky2016-05-30 13:47:12
PHP
Igor Shumilovsky, 2016-05-30 13:47:12

How to teach express to execute php?

Hello! Guys, this question:
I'm using express + webpack to build a project and now I need to send Ajax requests to php files. Are there any options for how to do this in express (express is used only in dev) or maybe somehow configure a proxy only for these ajaxes?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
E
Eugene 222, 2016-05-30
@mik222

You mixed everything together and in the end nothing is clear.
-------
If I understand you correctly, you need to send GET/POST requests from your node.js application to your PHP application.
--------
If so, then it is done as follows

var request = require('request')
var url = 'http://www.google.com' // input your url here

// use a timeout value of 10 seconds
var timeoutInMilliseconds = 10*1000
var opts = {
  url: url,
  timeout: timeoutInMilliseconds
}

request(opts, function (err, res, body) {
  if (err) {
    console.dir(err)
    return
  }
  var statusCode = res.statusCode
  console.log('status code: ' + statusCode)
})

N
Nicholas, 2016-05-30
@ACCNCC

https://www.npmjs.com/package/require

S
Super User, 2016-05-30
@sergeystepanov1988

Express doesn't work with php. It's actually a node.js framework. If you want to work with PHP, use Open Server for example.

D
Denis, 2015-09-03
@Sc0undRel

Because three hundred and two.

P
Pavel Volintsev, 2015-09-03
@copist

Take libraries like Guzzle or at least just curl .
They work more correctly with the HTTP protocol than file_get_contents

<?php

$link = "http://www.aliexpress.com/item/Wholesale-price-free-shipping-good-quality-high-clear-phone-back-screen-protect-film-For-Huawei-Ascend/32258530279.html";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt( $ch, CURLOPT_URL, $link );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
$ali = curl_exec( $ch );
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);

if ($error) // ошибка curl, например таймаут
{
    echo 'CURL error occurred during the request: ' . $error;
    echo "\n";
} elseif ($http_code<200 || $http_code>=300) // код возврата не 200
{
    echo 'HTTP error ' . $http_code. ' occurred during the request';
    echo "\n";
    var_dump(curl_getinfo( $ch )); // там все заголовки и другая отладочная информация
} else
{
    print_r($ali);
}

$client = new GuzzleHttp\Client();
$res = $client->get('http://www.aliexpress.com/item/Wholesale-price-free-shipping-good-quality-high-clear-phone-back-screen-protect-film-For-Huawei-Ascend/32258530279.html');
echo $res->getStatusCode();
// "200"
echo $res->getHeader('content-type');
// 'text/htm; charset=utf8'
echo $res->getBody();
// <html ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question