打开网址时,CURL会更改非英文字符

时间:2016-01-23 13:27:54

标签: php curl non-english

我正在尝试打开一个带有curl end的url回显该页面中的文本。 url将参数发送到python脚本,并根据参数创建文件。所以,我通过ajax向wordpress函数发送参数,这部分工作,它获取参数没有任何错误,然后我用这个创建url并使用curlopt_url在后端打开它。然后回应响应以将数据发送到ajax。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

typedef struct Tree {
    int val;
    char *word;
    struct Tree *left;
    struct Tree *right;
} Tree;

void show(Tree *hd) {
    if (hd != NULL) {
        show(hd->left);
        show(hd->right);
        printf("%s -- %d\n", hd->word, hd->val);
    }
}

void zero(Tree *aTree) {
    if (aTree == NULL)
        return;

    zero(aTree->left);
    free(aTree);
    zero(aTree->right);    
}

int alpha(char *word1, char *word2) {
    if (word1[0] == 0 && word2[0] == 0)
        return 2;
    else
    if (word1[0] == word2[0])
        return alpha(&word1[1], &word2[1]);
    else
    if (word1[0] < word2[0])
        return 1;
    else
        return 0;
}

Tree *create(char *word) {
    Tree *temp;
    temp = (Tree*)malloc(sizeof(Tree));
    temp->left = temp->right =  NULL;
    temp->val = 1;
    temp->word = (char*)malloc(sizeof(char));
    strcpy(temp->word, word);
    return temp;
}

Tree *insert(Tree *aTree, char *word) {
    if (aTree == NULL) {
        aTree = create(word);
    } else
    if (alpha(aTree->word, word) == 0) {
        aTree->left = insert(aTree->left,word);
    } else
    if (alpha(aTree->word, word) == 1) {
        aTree->right = insert(aTree->right, word);
    } else
    if (alpha(aTree->word, word) == 2) {
        aTree->val++;
    }
    return aTree;
}

int main(int argc, char *argv[]) {
    Tree *myTree = NULL;
    char buffer[256] = { 0 };
    char temp = 0;
    int i = 0;
    FILE *fp = fopen(argv[1], "r");
    if (fp)  {
        while (temp != EOF) {
            temp = getc(fp);
            temp = toupper(temp);
            if (temp >= 65 && temp <= 90) {
                buffer[i] = temp;
                i++;
            } else {
                if (buffer[0] != 0) {
                    puts(buffer);
                    myTree = insert(myTree, buffer);
                    memset(buffer, 0, sizeof(buffer));
                    i = 0;
                }
            }
        }
    }
    fclose(fp);
    show(myTree);
    return 0;
}

假设p2包含非英语字符,如ş,ğ,ı等。函数正确获取它(当我回显$ url而不是响应时我可以看到它),但在CURLOPT_URL部分它们正在改变。 另外,当我打开url表单浏览器时,它工作正常。

1 个答案:

答案 0 :(得分:0)

从OP成功评论我添加我的解决方案作为答案将有助于未来的访问者:

您需要在网址中使用urlencode作为非英文字符。

$param2 = urlencode($_REQUEST['p2']);

旁注:

而不是使用特定字段或参数上的完整网址编码,但有时您需要在特殊字符情况下编码完整网址。